EE 308 Feb. 11, 2002Some C basicsEvery C program has a function main()– The simplest C program is:main(){}Every statement ends with a semicolonx = a+b;Comment starts with /* ends with *//* This is a comment */Simple program – increment Port A#include "hc12b32.h"main(){DDRA = 0xff; /* Make PORTA output */PORTA = 0; /* Start at 0 */while(1) /* Repeat forever */{PORTA = PORTA + 1;}}Data Types:8-bit 16-bitunsigned char unsigned intsigned char signed int1EE 308 Feb. 11, 2002Need to declare variable before using it:signed char c;unsigned int i;Can initialize variable when you define it:signed char c = 0xaa;signed int i = 1000;– You tell compiler it you are using signed or unsiged numbers; the com-piler will figure out whether to use BGT or BHIArrays:unsigned char table[10]; /* Set aside 10 bytes for table */– Can refer to elements table[0] through table[9]– Can initialize an array when you define it:table[] = {0xaa, 0x55, 0xa5, 0x5a};Arithmetic operators:+ (add) x = a+b;- (subtract) x = a-b;* (multiply) x = a*b;/ (divide) x = a/b;% (modulo) x = a%b; (Remainder on divide)Logical operators& (bitwise AND) y = x & 0xaa;| (bitwise OR) y = x | 0xaa;ˆ (bitwise XOR) y = x ˆ 0xaa;<< (shift left) y = x << 1;>> (shift right) y = x >> 2;˜ (1’s complement) y = ˜x;- (2’s complement - negate) y = -x;2EE 308 Feb. 11, 2002Check for equality - use ==if (x == 5)Check if two conditions true:if ((x==5) && (y==10))Check if either of two conditions true:if ((x==5) || (y==10))Assign a name to a number#define COUNT 5Include a header file (such as hc12b32.h:#include "hc12b32.h"Declare a function: Tell what parameters it uses, what type of number itreturns:int read_port(int port);If a function doesn’t return a number, declare it to be type voidvoid delay(int num);3EE 308 Feb. 11, 2002Setting and Clearing Bits using Assembly and CTo put a specific number into a memory location or register (e.g., to put 0x55into PORTA):– In assembly:ldaa #$55staa PORTA– In C:PORTA = 0x55;To set a particular bit of a register (e.g., set Bit 4 of PORTA) while leavingthe other bits unchanged:– In assembly, use the bset instruction with a mask which has 1’s in thebits you want to set:bset PORTA,#$10– In C, do a bitwise OR of the register with a mask which has 1’s in the bitsyou want to set:PORTA = PORTA | 0x10;To clear a particular bit of a register (e.g., clear Bits 0 and 5 of PORTA) whileleaving the other bits unchanged:– In assembly, use the blcr instruction with a mask that has 1’s in the bitsyou want to clear:bclr PORTA,#$21– In C, do a bitwise AND of the register with a mask that has 0’s in the bitsyou want to clear:PORTA = PORTA & 0xDE;orPORTA = PORTA & ˜0x21;4EE 308 Feb. 11, 2002Sum the odd 8-bit numbers in an arrayWrite a program to sum all the odd numbers in an array of data.The numbers in the array should be treated as unsigned 8-bit numbers.The array starts at address 0xE000 and ends at address 0xE01F.This is the same program which was done in assembly language on Feb. 1,2002.STARTProcessEntriesInitInitDone0 −> SumAddr −>PointerDoneSaveAnswerSUM ODD 8−BIT NUMBERS IN ARRAY FROM 0xE000 TO 0xE01f0xE0000xE01F 4 5 1 8 611ProcessEntriesInitGetNumIncPointerAdd Numto SumMoreto do?NoYesOdd?YesNo5EE 308 Feb. 11, 2002Convert to C0xE0000xE01F 4 5 1 8 611ptr −>STARTProcessEntriesInitInitDone0 −> SumAddr −>PointerDoneSaveAnswerSUM ODD 8−BIT NUMBERS IN ARRAY FROM 0xE000 TO 0xE01fProcessEntriesInitGetNumIncPointerAdd Numto SumMoreto do?NoYesHow to check if odd?Divide by 2, if REM = 1 oddModulo (%) in C returns REMif−then do−whileOdd?NoYesx = *ptr;if ((x % 2) == 1) {sum = sum + x;}sum = 0;do {ptr = ptr + 1;}main() {}ptr = (unsigned char *) 0xe000;while (ptr <= (unsigned char *) 0xe01f);6EE 308 Feb. 11, 2002/* Program to sum the odd numbers in an array* The numbers are unsigned characters* The array starts at address 0xE000 and* ends at adress 0xE01F*/#define START 0xE000#define END 0xE01Fmain(){unsigned int sum; /* Keep the running sum */unsigned char *ptr; /* Pointer to array element */unsigned char x; /* Character from array */ptr = (unsigned char *) START;sum = 0;do{x = *ptr; /* Get entry */if ((x % 2) == 1) /* Is number odd? */{sum = sum + x; /* Odd: add to sum */}ptr = ptr + 1; /* Advance to next */}while (ptr <= (unsigned char *) END); /* Done? */}7EE 308 Feb. 11, 2002Count the number of negative numbers in an arrayPROCESSTABLE POINTERINITIALIZEPROCESSENTRYTABLEENDINITIALIZENEG CNTR.ptrENDENTRIESINITIALIZECOUNTINITIALIZENEGSNEXT ENTRYENDYESPTR <=TBL END?YESNONOENTRYNEG?ADVANCE TOINC NEG CNTR8EE 308 Feb. 11, 2002Convert to CPROCESSTABLE POINTERENDINITIALIZENEG CNTR.ptrENDENTRIESINITIALIZEINITIALIZENEGSCOUNTINITIALIZESTARTENDptr = START;count = 0;PROCESSENTRYNEXT ENTRYENDYESPTR <=TBL END?YESNONOENTRYNEG?ADVANCE TOINC NEG CNTRdo {if (*ptr < 0) {count = count + 1;ptr = ptr + 1;while (ptr <= END);}}9EE 308 Feb. 11, 2002/** Program to count the number of* 8-bit negative numbers from* 0x8000 to 0xBFFF*/#define START 0x8000#define END 0xBFFFmain(){int count;signed char *ptr;count = 0;ptr = (signed char *) START;do{if (*ptr < 0){count = count + 1;}ptr = ptr + 1;}while (ptr <= (signed char *) END);}10EE 308 Feb. 11, 2002A software delayTo enter a software delay, put in a nested loop, just like in assembly.– Write a function delay(num) which will delay for num millisecondsvoid delay(unsigned int num){unsigned int i;while (num > 0){i = XXXX;/* ------------------------ */while (i > 0) /* */{ /* Want inner loop to delay */i = i - 1; /* for 1 ms */} /* *//* ------------------------ */num = num - 1;}}What should XXXX be to make a 1 ms delay?11EE 308 Feb. 11, 2002Look at assembly listing generated by compiler:; 27 void delay(int num); 28 {switch .text_delay:pshdpshdOFST: set 2bra L55L35:; 33 i = XXXX;ldy #XXXX--------------------------------------------------------inner | L16:loop | ; 36 i = i-1;takes | dey6 | sty OFST-2,scycles | ; 34 while (i > 0)| bgt L16--------------------------------------------------------; 38 num = num - 1;ldy OFST+0,sdeysty OFST+0,sL55:; 31 while (num>0)ldd OFST+0,sbgt L35; 40 }leas 4,srtsxdef _mainxdef _delay12EE 308 Feb. 11, 2002Inner loop takes 6 cyles.One millisecond takes 8,000 cycles(8,000,000 cycles/sec1 millisecond = 8,000 cycles)Need to execute inner loop 8,000/6 = 1,333 times to dealy for 1 millisecondvoid delay(unsigned int num){unsigned int i;while (num > 0){i =
View Full Document