
































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A syllabus for a course on programming in c for the 8051 microcontroller using the keil compiler. It covers topics such as the keil compiler, variable extensions, memory types, and register definitions. The course includes examples of how to access the serial port and how to use interrupts. The document also includes example code for initializing the serial port and for sending and receiving data.
Typology: Study notes
1 / 40
This page cannot be seen from the preview
Don't miss anything!

































/* declare two bit variables - the compiler will decide which / / addresses they are at. Initialize them to 0 and 1. / bit testbit1 = 0; bit testbit2 = 1; / set testbit1 to the value in testbit2 / testbit1 = testbit2; / clear testbit2 / testbit2 = 0; / testbit1 is now a 1, and testbit2 is now a 0 / / Note that the assignment of testbit2 to testbit1 only copied / / the contents of testbit2 into testbit1. It did not change / / the location of testbit1 to be the same as testbit2. */
/* create an sbit variable that points to pin 0 of port 1 / / note that this is done outside of any functions! / sbit P10 = 0x90; / now the functions may be written to use this location / void main (void) { / forever loop, toggling pin 0 of port 1 / while (1==1) { P10 = !P10; delay (500); / wait 500 microseconds */ } }
/* This is a function that will calculate and return a checksum of / / a range of addresses in code memory, using a simple algorithm / / that simply adds each consecutive byte together. This could be / / useful for verifying if the code in ROM got corrupted (like if / / the Flash device were wearing out). / unsigned int checksum (unsigned int start, unsigned int end) { / first, declare pointers to the start and end of / / the range in code memory. */ unsigned int code *codeptr, codeend; / now declare the variable the checksum will be / / calculated in. Because direct-addressable data / / is faster to access than indirect, and this / / variable will be accessed frequently, we will / / declare it in data memory (instead of idata). / / In reality, if left unspecified, the compiler / / would probably leave it in the accumulator for / / even faster access, but that would defeat the / / point of this example. / unsigned int data checksum = 0; / Initialize the codestart and codeend pointers to / / the addresses passed into the function as params. / / because start and end are passed in as values, / / not pointers, they must be cast to the correct / / pointer type */ codeptr = (unsigned int code *)start; codeend = (unsigned int code )end; / Now perform the checksum calculation, looping */
/* This is a function that will be called whenever a serial / / interrupt occurs. Prior to executing the handler, the / / processor will switch to register bank 1 void serial_int (void) interrupt 4 using 1 { ... }
/* Because this function may be called from both the main program / / and an interrupt handler, it is declared as reentrant to / / protect its local variables. / int somefunction (int param) reentrant { ... return (param); } / The handler for External interrupt 0, which uses somefunction() / void external0_int (void) interrupt 0 { ... somefunction(0); } / the main program function, which also calls somefunction() */ void main (void) { while (1==1) { ... somefunction(); } }
main() { unsigned int i; /* will be used for a delay loop / / First, Port 0 will be initialized to zero / P0 = 0; /
#include /* function declarations / char getCharacter (void); / read a character from the serial port / void sendCharacter (char); / write a character to the serial port / /
while (TI != 1) {;} /*
sbit load = P0^6; sbit Start_LCD = P0^7; /* bit 7 of Port 3 / / Delay function / void delay() { int i, j; for(i=0; i<1000; i++) for(j=0; j<100; j++) i = i + 0; } / Function to output the decimal value of the count on the LCD / void PrintInt(unsigned char i) { char ch[4]; / Write code to convert the count to a string value and use the PrintString function provided in io.c / PrintString(ch); } void main(void) { unsigned char count = 0; InitIO(); / Initialize the LCD / while (1) { if (Start_LCD == 1) { ClearScreen(); PrintString("Ready..."); delay(); } else if (reset == 1) { / Output 0 on the LCD / } else if (load == 1) { / Output the current value of Datain on the LCD / } else { / Check the Up/Down pin for 1 or 0 count up or down accordingly. Display each value on the LCD */ } } }
P:\Peart\cs