VojtVojtěěchch KrmKrmííččekek vojtec@vojtec@icsics..munimuni..czcz PIC12Fxxx Programming PIC in C PIC12FxxxPIC12Fxxx ProgrammingProgramming PIC in CPIC in C CC IntroductionIntroduction WWe can use high level language C to avoide can use high level language C to avoid writing a code in assembly codewriting a code in assembly code C compiler converts C code to theC compiler converts C code to the assemassembblerler codecode WeWe cancan useuse debuggerdebugger toolstools in MPLABin MPLAB forfor debuggingdebugging ­­ steppingstepping,, watchwatch,, memorymemory listinglisting,, disassemlbydisassemlby listinglisting,, ...... Logic Operations in CLogic Operations in CLogic Operations in C Operations with bits:Operations with bits: && -- ANDAND || -- OROR ^^ -- XORXOR <<<< -- shift to leftshift to left >>>> -- shift to rightshift to right ~~ -- negationnegation Operations with bytes:Operations with bytes: &&&& -- ANDAND |||| -- OROR ExpressingExpressing NumbersNumbers NumberNumber DefinitionsDefinitions unsigned char x;unsigned char x; //// 8 bits on PIC8 bits on PIC x=255;x=255; //// givegive decimaldecimal numbernumber x=0xFF;x=0xFF; //// give hex number, x=255give hex number, x=255 x=0b11111111;x=0b11111111; //// give binary number, x=255give binary number, x=255 WhatsWhats wrong with the following code?wrong with the following code? unsigned char i;unsigned char i; ffor (i=7;i>=0;ior (i=7;i>=0;i----) { }; //will loop forever) { }; //will loop forever AvailableAvailable DatatypesDatatypes on a PICon a PIC unsigned char a; //8 bits, 0 to 255unsigned char a; //8 bits, 0 to 255 signed char b; //8 bits,signed char b; //8 bits, --128 to 127128 to 127 unsignedunsigned intint c; //16 bits, 0 to 65535c; //16 bits, 0 to 65535 signedsigned intint d; //16 bits,d; //16 bits, --32768 to 3276732768 to 32767 long e; //32 bits,long e; //32 bits, --2147483648 to2147483648 to 21474836472147483647 float f; //24 or 32 bits, depending onfloat f; //24 or 32 bits, depending on options underoptions under ``edit projectedit project'' bit g;bit g; // 1bit, 0// 1bit, 0 oror 11 SwitchingSwitching Bit inBit in VariableVariable unsigned char x = 0b10010101 // var. definition Setting bit number bitno: x = x | (1 << bitno); Clearing bit number bitno: x = x & ~(1 << bitno); We can use macros: #define bit_set(var,bitno) ((var) |= 1 << (bitno)) #define bit_clr(var,bitno) ((var) &= ~(1 << (bitno))) Handling InterrputsHandlingHandling InterrputsInterrputs compilercompiler automaticallyautomatically managesmanages savingsaving ofof contextcontext (STATUS(STATUS andand WW registersregisters)) void interruptvoid interrupt tc_int(voidtc_int(void)) {{ if (T0IE && T0IF) {if (T0IE && T0IF) { T0IF=0;T0IF=0; ++++tick_counttick_count;; }} }}