pic 单片机 ccs 之c 语言 中文版权归芜湖联合大学 94 电气...

Download PIC 单片机 CCS 之C 语言 中文版权归芜湖联合大学 94 电气 …read.pudn.com/downloads121/ebook/515912/PICC_mcu_CCS.pdf · struct{ short int r_w; //定义位变量 short

If you can't read please download the document

Upload: dotuyen

Post on 06-Feb-2018

283 views

Category:

Documents


17 download

TRANSCRIPT

  • PIC CCS C 94 ,,,,

    :13662655920 1

    #BIT : #bit id=x.y

    id C ; x C ; y ( 0~7)

    : C (),, x y , C CPU . C . :

    #bit T0IF = 0xb.2 // T0IF 0xb.2 T0IF=0; // Timer0 int result; #bit result_odd = result.0 // result_odd result.0 if(result_odd){;} // result_odd 1,

    : ex_glint.c #if defined(__PCM__) // PCM , defined(__PCM__) 1 #include // 16F877.h #fuses HS,NOWDT,NOPROTECT,NOLVP //HS:/, NOWDT: WDT // NOPROTECT: #use delay(clock=20000000) //:delay_ms() delay_us() //#USE DELAY()#use rs232(). #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) // 9600,

    // PIN_C6 // PIN_C7 //:GETC,PUTC PRINTF;

    #elif defined(__PCH__) #include #fuses HS,NOWDT,NOPROTECT,NOLVP #use delay(clock=20000000) #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) #endif // if long counter; // counter long #if defined(__PCM__) // PCM , defined(__PCM__) 1 int save_w; // save_w int #locate save_w=0x7f // save_w RAM ,0x7f , W int save_status; // save_status int , status #locate save_status=0x20 // save_status RAM , 0x20 , #byte status = 3 // status 3, #bit zero_flag = status.2 // zero_flag status.2, status Z #bit t0if = 0xb.2 // t0if 0xb.2, timer0 #elif defined(__PCH__)

  • PIC CCS C 94 ,,,,

    :13662655920 2

    int save_w; #locate save_w=0x80 int save_status; #locate save_status=0xFF #byte status = 0xFD8 #bit zero_flag = status.2 //status 0 , Z #bit t0if = 0xFF2.2 #endif // if #INT_GLOBAL // void isr() { #asm // //store current state of processor MOVWF save_w // W save_w , W, W SWAPF status,W // status , W BCF status,5 // status 5 0, RP0=0 BCF status,6 // status 6 0, RP1=0, bank0 MOVWF save_status // W save_status , status, status BCF t0if // timer0 t0if 0 INCF counter,F // counter 1 counter BTFSC zero_flag // counter 0x00, zero_flag=0,;

    // counter=0x00,zero_flag=1,; INCF (&counter+1),F // counter 1, (&counter+1) 1 SWAPF save_status,W // save_status , W MOVWF status // W status , status SWAPF save_w,F // save_w, save_w SWAPF save_w,W // save_w , W , W #endasm // } void main() {

    printf("\r\nStarting the counter.\r\n\n"); counter = 0;

    setup_counters(RTCC_INTERNAL,RTCC_DIV_32); // Timer0 // 32 ,TMR0 1

    enable_interrupts(INT_RTCC); // Timer0(RTCC), enable_interrupts(GLOBAL); // while(TRUE) { printf("The counter value is currently: %5lu\r\n", counter); delay_ms(1000); // 1000ms } } // timer0 32 .

  • PIC CCS C 94 ,,,,

    :13662655920 3

    #BYTE : #byte id=x

    id C ; x C ;

    : id C , x ,,. id C ,#byte id=x C , x ,(8 ).

    :,x (id).., x , id x . :

    #byte status=3 // status 3 #byte b_port=6 // b_port 6 struct{ short int r_w; // short int c_d; // int unused :2; // int data :4; // 4 }a_port // a_port , r_w a_port.0, c_d a_port.1 #byte a_port=5 // a_port 5, RA a_port.c_d=1;

    : ex_glint.c #DEFINE : #define id text

    or #define id(x,y) text

    id ;text ;x,y ,,. : id,.

    , text , text ;

    text #idx , id x ; text idx##idy , idx idy

    ; : #define BITS 8 // BITS 8

    a=a+BITS; // a=a+8 #define hi(x) (x

  • PIC CCS C 94 ,,,,

    :13662655920 4

    // NOPROTECT: #use delay(clock=20000000) //:delay_ms() delay_us() //#USE DELAY()#use rs232(). #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) // 9600,

    // PIN_C6 // PIN_C7 //:GETC,PUTC PRINTF;

    #elif defined(__PCH__) #include #fuses HS,NOWDT,NOPROTECT,NOLVP #use delay(clock=20000000) #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) // Jumpers: 8 to 11, 7 to 12 #endif // if #define INTS_PER_SECOND 76 // (20000000/(4*256*256)) INTS_PER_SECOND 76 BYTE seconds; //,1 =(4/20000000)*256*256* INTS_PER_SECOND BYTE int_count; // 1 ,, 1 ; #int_rtcc //Timer0(RTCC), void clock_isr() { // the RTCC (timer0) overflows (255->0),For this program this is apx 76 times

    if(--int_count==0) { // per second. ++seconds; //1 , seconds 1; int_count=INTS_PER_SECOND; //// int_count } } void main() { BYTE start; int_count=INTS_PER_SECOND; // int_count set_timer0(0); ///

    setup_counters( RTCC_INTERNAL, RTCC_DIV_256 | RTCC_8_BIT); // Timer0

    // 256 ,TMR0 1 // PIC18XXXX ,RTCC_8_BIT Timer0 8 ; enable_interrupts(INT_RTCC); // Timer0(RTCC), enable_interrupts(GLOBAL); // do { printf("Press any key to begin.\n\r"); getc(); // RS232 1 start=seconds; printf("Press any key to stop.\n\r"); getc(); // RS232 1 printf("%u seconds.\n\r",seconds-start); } while (TRUE); } Timer0 , 256 , 1 .

  • PIC CCS C 94 ,,,,

    :13662655920 5

    : ex_macro.c : #define BUFFERSIZE 10 // BUFFERSIZE 10 #define BUFFER_EMPTY (next_in==next_out)

    // BUFFER_EMPTY (next_in==next_out) #define min(x,y) ((xy, x #define forever while(1); // forever while(1) #define MHZ(x) x##000000 // MHZ(x) x##000000 #ifndef __PCB__ //__PCB__,; #define NORMAL_RS232 baud=9600, xmit=PIN_C6, rcv=PIN_C7 // #else //__PCB__,; #define NORMAL_RS232 baud=9600, xmit=PIN_B1, rcv=PIN_B0 #endif // if #define set_options(value) {#ASM \ MOVLW value \ OPTION \ #ENDASM} #define debug(x) printf("%s variable value is %d\r\n",#x,x); // debug(x) printf("%s variable value is %d\r\n",#x,x);; #define TOSTRING(s) #s // TOSTRING(s)#s #define DEVICE_FILE_FOR(chip) TOSTRING(chip##.h) #ifdef __pcb__ //__PCB__,; #define IDLE {if(kbhit()) isr();} // IDLE {if(kbhit()) isr();}

    // getc(), kbhit() 1,, kbhit() 0 #else //__PCB__,; #define IDLE ; // IDLE ; ; #endif // if #include DEVICE_FILE_FOR(16C74) // 16C74.h #fuses HS,NOPROTECT //HS:/, NOPROTECT: #use delay(clock=MHZ(20)) //:delay_ms() delay_us() //#USE DELAY()#use rs232(). #use RS232(NORMAL_RS232) // 9600,

    // PIN_C6 // PIN_C7 //:GETC,PUTC PRINTF, kbhit();

    int buffer[BUFFERSIZE]; // buffer[10] int next_in, next_out; // next_in next_out #ifndef __pcb__ //__PCB__,; #int_rda //RS232 , #endif // if

  • PIC CCS C 94 ,,,,

    :13662655920 6

    void isr() { buffer[next_in]=getc(); // RS232 next_in=(next_in+1)%BUFFERSIZE; // next_in 1 , 10, next_in } void main() { int x, largest; // x largest

    set_options(0x34); // option , Timer0 // 1:32, Timer0 ; // Timer0 ;INT // GPIO

    #ifndef __pcb__ //__PCB__,; enable_interrupts(INT_RDA); // UART enable_interrupts(GLOBAL); // #endif // if next_in=next_out=0; // next_in next_out largest=0; // largest do { while(BUFFER_EMPTY) // next_in=next_out

    IDLE; //__PCB__, IDLE {if(kbhit()) isr();} // getc(), kbhit() 1,, kbhit() 0 //__PCB__, IDLE ;

    x=buffer[next_out]; next_in=(next_out+1)%BUFFERSIZE; // next_out 1 , 10, next_in largest = max(largest,x); // debug(next_in); // debug(x) printf("%s variable value is %d\r\n",#x,x);; debug(next_out) // debug(x) printf("%s variable value is %d\r\n",#x,x);; } forever; // forever while(1) } UART 10 , #DEVICE : #device chip options

    chip (:PIC16C74),: START | RUN | CCSC +Q Option .: *=5 5 () *=8 8 ( 14 16 ) *=16 16 ( 14 ) ADC=x x read_adc(). ICD=TRUE Microchips ICD WRITE_EEPROM=ASYNC EEPROM , EEPROM .,

  • PIC CCS C 94 ,,,,

    :13662655920 7

    ISR ISR EEPROM chip options ,#device .: `#device #device #fuse . :, chip #device. :

    #device PIC16C74 // PIC16C74 #device PIC16C67 *=16 // PIC16C67, 16 #device *=16 ICD=TRUE // 16 , ICD #device PIC16F877 *=16 ADC=10 // read_adc() 10

    :ex_mxram.c, ex_icd.c, 16c74.h : ex_mxram.c : #if defined( __PCB__) // PCB , defined( __PCB__) 1 #include // 16C57.H #device *=8 // This allows auto variables over location 0x1F // 8 ( 14 16 ) #fuses HS, NOWDT, NOPROTECT //HS:/, NOWDT: WDT // NOPROTECT: #use delay(clock=20000000) //:delay_ms() delay_us() //#USE DELAY()#use rs232(). #use rs232(baud=9600, xmit=PIN_A3, rcv=PIN_A2) // 9600,

    // PIN_A3 // PIN_A2

    //:GETC,PUTC PRINTF, kbhit(); #define BUFFSIZE 16 // BUFFERSIZE 10 #elif defined(__PCM__) #include #device *=16 // This allows auto variables over location 0xFF #fuses HS,NOWDT,NOPROTECT,NOLVP #use delay(clock=20000000) #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) // Jumpers: 8 to 11, 7 to 12 #define BUFFSIZE 64 #endif // if byte buffer1[BUFFSIZE],buffer2[BUFFSIZE],buffer3[BUFFSIZE]; // Three buffers are allocated because C requires arrays elements to be in // consecutive memory locations and the PIC memory has holes. // buffer1[BUFFSIZE], buffer2[BUFFSIZE], buffer3[BUFFSIZE] // The following allows reads and writes to the three arrays like they are // one large array. void write_buffer(char * index, char value) { if(index

  • PIC CCS C 94 ,,,,

    :13662655920 8

    else if(index

  • PIC CCS C 94 ,,,,

    :13662655920 9

    #define TOGGLE_PIN PIN_B0 // TOGGLE_PIN PIN_B0 // This function turns the LEDs on and off LED void flash_led() { output_low(TOGGLE_PIN); // TOGGLE_PIN (PIN_B0) delay_ms(500); // 500ms output_high(TOGGLE_PIN); // TOGGLE_PIN (PIN_B0) delay_ms(500); // 500ms } void main() { int a, b, c; // a, b c long al, bl, cl; // Long a1, b1 c1 a=10; // a 10 b=25; // b 25 al=300; // a1 300 bl=525; // b1 525 c=a+b; // c=a*b; // cl=al+bl; cl=al*bl; while(TRUE) flash_led(); //LED 1Hz } delay_ms(), output_high(), output_low() #FUSES : #fuse options

    options . devices.h ,.PCW fuses.PCW VIEW|Valid fuses fuses .

    : LP, XT, HS, RC WDT, NOWDT PROTECT, NOPROTECT PUT, NOPUT, (Power Up Timer) BROWNOUT, NOBROWNOUT

    : fuses .,. Parallax fuses, PAR .SWAP Hex .,.

    fuses,, fuse. 18F452. fuse PROTECT=6 6 CONFIG5L, 0 3 :

    #fuses HS, NOWDT :ex_sqw.c

  • PIC CCS C 94 ,,,,

    :13662655920 10

    : ex_sqw.c : #if defined(__PCM__) // PCM , defined( __PCM__) 1 #include // 16F877.h #fuses HS,NOWDT,NOPROTECT //HS:/, NOWDT: WDT // NOPROTECT: #use delay(clock=20000000) //:delay_ms() delay_us() //#USE DELAY()#use rs232(). #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

    // 9600, // PIN_C6 // PIN_C7 //:GETC,PUTC PRINTF, kbhit();

    #elif defined(__PCB__) #include #fuses HS,NOWDT,NOPROTECT,NOLVP #use delay(clock=20000000) #use rs232(baud=9600, xmit=PIN_A3, rcv=PIN_A2) // Jumpers: 11 to 17, 12 to 18 #elif defined(__PCH__) #include #fuses HS,NOWDT,NOPROTECT,NOLVP #use delay(clock=20000000) #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) // Jumpers: 8 to 11, 7 to 12 #endif // if void main() { printf("Press any key to begin\n\r"); getc(); printf("1 khz signal activated\n\r"); while (TRUE) { output_high(PIN_B0); //(PIN_B0) delay_us(500); // 500us output_low(PIN_B0); //(PIN_B0) delay_us(500); // 500us } } delay_us(), output_high(), output_low() #IF expr #ELSE #ELIF #ENDIF : #if expr

    code

  • PIC CCS C 94 ,,,,

    :13662655920 11

    #elif expr code #else code #endif expr ,; Code C .

    : , 0 ,#ELSE #ENDIF.

    :#IF C ,#define . id , DEFINED(id) 1, id,

    DEFINED(id) 0. :#if MAX_VALUE>255

    long value; // MAX_VALUE>255, value #else int value; // MAX_VALUE 255, value #endif

    :ex_extee.c : ex_extee.c : #if defined(__PCB__) // PCB , defined( __PCB__) 1 #include // 16c56.h #fuses HS, NOWDT, NOPROTECT //HS:/, NOWDT: WDT // NOPROTECT: #use delay(clock=20000000) //:delay_ms() delay_us() //#USE DELAY()#use rs232(). #use rs232(baud=9600, xmit=PIN_A3, rcv=PIN_A2)

    // 9600, // PIN_A3 // PIN_A2 //:GETC,PUTC PRINTF, kbhit();

    #elif defined(__PCM__) #include #fuses HS,NOWDT,NOPROTECT,NOLVP #use delay(clock=20000000) #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) // Jumpers: 8 to 11, 7 to 12 #elif defined(__PCH__) #include #fuses HS,NOWDT,NOPROTECT,NOLVP #use delay(clock=20000000) #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) // Jumpers: 8 to 11, 7 to 12 #endif // if #include // input.c #include // 2416.c void main() {

  • PIC CCS C 94 ,,,,

    :13662655920 12

    BYTE value, cmd; // value, cmd EEPROM_ADDRESS address; // EEPROM_ADDRESS long int, 16 init_ext_eeprom(); // eeprom I/O do { do { printf("\r\nRead or Write: "); cmd=getc(); // RS232 cmd=toupper(cmd); // cmd cmd putc(cmd); } while ( (cmd!='R') && (cmd!='W') ); // R W

    printf("\n\rLocation: ");

    #if sizeof(EEPROM_ADDRESS)==1 // EEPROM_ADDRESS 1 , sizeof(EEPROM_ADDRESS)==1 1 address = gethex(); #else // EEPROM_ADDRESS 1 #if EEPROM_SIZE>0xfff address = gethex(); #else // EEPROM_SIZE 0xfff address = gethex1(); // RS232 , eeprom #endif // if address = (address

  • PIC CCS C 94 ,,,,

    :13662655920 13

    return((toupper(digit)-'A')+10); // ascii 39, 41, 10 } BYTE gethex() { int lo, hi; // lo, hi hi = gethex1(); // RS232 , hi lo = gethex1(); // RS232 , lo if(lo==0xdd) return(hi); else return( hi*16+lo ); } void get_string(char* s, int max) { int len; // len char c; // c --max; // max len=0; // len do { c=getc(); // RS232 , c if(c==8) { // Backspace if(len>0) { len--; putc(c); // RS232 c putc(' '); putc(c); } } else if ((c>=' ')&&(c

  • PIC CCS C 94 ,,,,

    :13662655920 14

    return(i); } signed long get_long() { char s[7]; // s[7] signed long l; // l get_string(s, 7); // RS232 7 , s l=atol(s); // s[] l return(l); } float get_float() { char s[20]; // s[7] float f; // l get_string(s, 20); // RS232 20 , s f = atof(s); // s[] f return(f); } #endif // if : 2416.c : //// Library for a MicroChip 24LC16B //// //// init_ext_eeprom(); Call before the other functions are used //// //// write_ext_eeprom(a, d); Write the byte d to the address a //// //// d = read_ext_eeprom(a); Read the byte d from the address a //// //// b = ext_eeprom_ready(); Returns TRUE if the eeprom is ready //// //// to receive opcodes //// //// The main program may define EEPROM_SDA //// //// and EEPROM_SCL to override the defaults below. //// //// Pin Layout //// //// ----------------------------------------------------------- //// //// | | //// //// | 1: NC Not Connected | 8: VCC +5V | //// //// | 2: NC Not Connected | 7: WP GND | //// //// | 3: NC Not Connected | 6: SCL EEPROM_SCL and Pull-Up | //// //// | 4: VSS GND | 5: SDA EEPROM_SDA and Pull-Up | //// //// ----------------------------------------------------------- //// #ifndef EEPROM_SDA // EEPROM_SDA, #define EEPROM_SDA PIN_C4 // EEPROM_SDA PIN_C4 #define EEPROM_SCL PIN_C3 // EEPROM_SCL PIN_C3 #endif // if #use i2c(master, sda=EEPROM_SDA, scl=EEPROM_SCL) // master // FORCE_HW, I2C .

  • PIC CCS C 94 ,,,,

    :13662655920 15

    // I2C_START, I2C_STOP #USE I2C . // I2C_READ, I2C_WRITE #USE I2C . // I2C_POLL #USE I2C .

    // sda EEPROM_SDA, scl EEPROM_SCL #define EEPROM_ADDRESS long int // EEPROM_ADDRESS long int #define EEPROM_SIZE 2048 // EEPROM_SIZE 2048 void init_ext_eeprom() { output_float(EEPROM_SCL); // EEPROM_SCL , output_float(EEPROM_SDA); // EEPROM_SDA , } BOOLEAN ext_eeprom_ready() { int1 ack; // ack i2c_start(); // ack = i2c_write(0xa0); // 0xa0; ack=0,(ACK);

    // ack=1,(NO ACK); i2c_stop(); // return !ack; } // ext_eeprom_ready(), 1,; 0, eeprom void write_ext_eeprom(long int address, BYTE data) { while(!ext_eeprom_ready()); //, i2c_start(); // i2c_write( (0xa0|(BYTE)(address>>7))&0xfe); //() i2c_write(address); // i2c_write(data); // i2c_stop(); // } BYTE read_ext_eeprom(long int address) { BYTE data; // data while(!ext_eeprom_ready()); //,, i2c_start(); //: i2c_write( (0xa0|(BYTE)(address>>7))&0xfe); //() i2c_write(address); // i2c_start(); // i2c_write( (0xa0|(BYTE)(address>>7))|1); //() data=i2c_read(0); // I2C , ack=0() i2c_stop(); // return(data); // I2C } 24C16, PC RS232

  • PIC CCS C 94 ,,,,

    :13662655920 16

    #IFDEF #IFNDEF #ELSE #ELIF #ENDIF : #ifdef id

    cide #elif code #else code #endif #ifndef id code #elif code #else code #endif

    id ,code C . :#IF .#IF , ID(#DEFINE ID), ;#IFDEF , ID ,, ID,#IFNDEF . :

    #define debug // debug #ifdef debug // debug , printf printf(debug poit a); #endif

    :ex_sqw.c #INCLUDE : #include

    #include filename filename PC ...encrypted

    PC .#include directive ,.,. :., project .,., . :#include

  • PIC CCS C 94 ,,,,

    :13662655920 17

    #include :ex_sqw.c #INT_GLOBAL : #int_global :. ,.#int_global ,,. :#int_global

    isr(){ // 0x0004 #asm bsf isr_flug retfie #endasm }

    :ex_glint.c #INT_DEFAULT : #int_default : PIC , .,,#int_default . :#int_default

    default_isr(){ printf(Unexplained interrupt\r\n);

    #INT_XXXX : #INT_AD //AD

    #INT_ADOF //AD #INT_BUSCOL // #INT_BUTTON // #INT_CCP1 // 1 #INT_CCP2 // 1 #INT_COMP // #INT_EEPROM //EEPROM #INT_EXT // #INT_EXT1 // 1 #INT_EXT2 // 2 #INT_I2C //I2C ( 14000) #INT_LCD //LCD #INT_LOWVOLT // #INT_PSP // PSP #INT_RB // B B4~B7

  • PIC CCS C 94 ,,,,

    :13662655920 18

    #INT_RC // C C4~C7 #INT_RDA //RS232 , UART #INT_RTCC //Timer0(RTCC) #INT_SSP //SPI I2C #INT_TBE //RS232 #INT_TIMER0 //Timer0(RTCC) #INT_TIMER1 //Timer1 #INT_TIMER2 //Timer2 #INT_TIMER3 //Timer3 :#INT_options . devices.h ,

    . :,.,, devices.h PCW ,VIEW|Valid ints .

    ,.,.,#INT_XXXX NOCLEAR .

    ENABLE_INTERRUPTS(INT_XXXX) , ENABLE_INTERRTUPTS(GLOBAL),.

    HIGH FAST PCH ,.. FAST ,,.HIGH.,#DEVICE .

    :#int_ad adc_handler(){ adc_active=FALSE; }

    #int_rtcc noclear isr(){

    }

    : ex_sisr.c ex_stwt.c : ex_stwt.c , : ex_sisr.c : #if defined(__PCM__) // PCM , defined( __PCM__) 1 #include // 16F877.h #fuses HS, NOWDT, NOPROTECT, NOLVP //HS:/, NOWDT: WDT // NOPROTECT: #use delay(clock=20000000) //:delay_ms() delay_us() //#USE DELAY()#use rs232(). #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

    // 9600, // PIN_C6 // PIN_C7

  • PIC CCS C 94 ,,,,

    :13662655920 19

    //:GETC,PUTC PRINTF, kbhit(); #elif defined(__PCH__) #include #fuses HS,NOWDT,NOPROTECT,NOLVP #use delay(clock=20000000) #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) // Jumpers: 8 to 11, 7 to 12 #endif // if #define BUFFER_SIZE 32 // BUFFER_SIZE 32 BYTE buffer[BUFFER_SIZE]; // buffer[BUFFER_SIZE] BYTE next_in = 0; // next_in, 0 BYTE next_out = 0; // next_out, 0 #int_rda //RS232 , void serial_isr() { int t; // t; buffer[next_in]=getc(); // RS232 1 , buffer[] t=next_in; // next_in t next_in=(next_in+1) % BUFFER_SIZE; //% if(next_in==next_out) next_in=t; // Buffer full !! } #define bkbhit (next_in!=next_out) // bkbhit (next_in!=next_out); BYTE bgetc() { BYTE c; // c; while(!bkbhit) ; // next_in=next_out ,, next_in next_out c=buffer[next_out]; next_out=(next_out+1) % BUFFER_SIZE; //% return(c); // c , } void main() { enable_interrupts(global); // enable_interrupts(int_rda); // UART 1 , printf("\r\n\Running...\r\n"); // The program will delay for 10 seconds and then display // any data that came in during the 10 second delay do { delay_ms(10000); // 10 printf("\r\nBuffered data => "); while(bkbhit) //, putc( bgetc() ); // UART } while (TRUE); } UART ,.

  • PIC CCS C 94 ,,,,

    :13662655920 20

    #TYPE : #type standard-type=size

    #type default=area standard-type short,int,long ; size 1,8,16 32; area #TYPE typemod . : short 1 , int 8 , long 16 . C INT . PIC ,INT 8 .,#type . #TYPE . :.#TYPE 4 ( 1 ), 4 :INT1,INT8,INT16 INT32.#TYPE, CCS .

    RAM . default=area , area typemod . :TYPE SHORT=8, INT=16, LONG=32

    #TYPE default=area typemoduser_ram_block; #type default= user_ram_block // 0x100~0x1ff #type default= //

    :ex_cust.c : ex_cust.c : #pragma case // C , #pragma type short=8,int=16,long=32 // short, int, long #define version 0x0001 // version 0x0001 #if defined(__PCB__) // PCB , defined( __PCB__) 1 #include // 16F877.h #pragma fuses HS, NOWDT, NOPROTECT //HS:/, NOWDT: WDT // NOPROTECT: #pragma use delay(clock=20000000) //:delay_ms() delay_us() //#USE DELAY()#use rs232(). #pragma use rs232(baud=9600, xmit=PIN_A3, rcv=PIN_A2)

    // 9600, // PIN_A3 // PIN_A2 //:GETC,PUTC PRINTF, kbhit();

    #define compiler __PCB__ // compiler __PCB__ #elif defined(__PCM__) #include

  • PIC CCS C 94 ,,,,

    :13662655920 21

    #pragma fuses HS,NOWDT,NOPROTECT,NOLVP #pragma use delay(clock=20000000) #pragma use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) // Jumpers: 8 to 11, 7 to 12 #define compiler __PCM__ #elif defined(__PCH__) #include #pragma fuses HS,NOWDT,NOPROTECT,NOLVP #pragma use delay(clock=20000000) #pragma use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) // Jumpers: 8 to 11, 7 to 12 #define compiler __PCH__ #endif // if #pragma id version #reserve 0x20:0x2f // RAM 0x20 0x2f //#reserve #DEVICE ,, int CYCLES; // Global cycles (note upper case) CYCLES 16 #inline //, void pulse_B0() { #pragma use fast_io(B) // I/O I/O

    // I/O , set_tris_X() I/O //.

    output_low(PIN_B0); //(PIN_B0) delay_ms(100); // 100ms output_high(PIN_B0); //(PIN_B0) delay_ms(100); // 100ms #pragma use standard_io(B) //, I/O,

    //,#USE XXX_io } #separate //,,

    // INLINE,#inline, void send_pulses() { int cycles; // Local cycles (note lower case) for(cycles=CYCLES; cycles!=0; --cycles) { pulse_B0(); } } #pragma zero_ram // #zero_ram 0,I/O void main() { output_low(PIN_B0); //(PIN_B0)

  • PIC CCS C 94 ,,,,

    :13662655920 22

    output_low(PIN_B1); //(PIN_B1) printf("Firmware version is %4X\r\n",version); printf("Compiled on %s with compiler version %s\r\n", __DATE__, compiler); printf("Pulsing B0 and B1..."); for(CYCLES=100;CYCLES

  • PIC CCS C 94 ,,,,

    :13662655920 23

    ), I/O RAM . :#use fixed_io(a_outputs=PIN_A2,PIN_A3) // PINA2 PIN_A3 #UNDEF :#undef id id #define id : id . :#if MAXSIZE

  • PIC CCS C 94 ,,,,

    :13662655920 24

    { short int status; i2c_start(); i2c_write(0xa0); i2c_write(address>>8); i2c_write(address); i2c_write(data); i2c_stop(); i2c_start(); status=i2c_write(0xa0); while(status==1) { i2c_start(); status=i2c_write(0xa0); } }

    BYTE read_ext_eeprom(long int address) { BYTE data; i2c_start(); i2c_write(0xa0); i2c_write(address>>8); i2c_write(address); i2c_start(); i2c_write(0xa1); data=i2c_read(0); i2c_stop(); return(data); }

    void Long_write_ext_eeprom(long int address, Long data) { int A; for (A=0;A

  • PIC CCS C 94 ,,,,

    :13662655920 25

    #USE RS232 :#use rs232(options) options ,: STREAM=id // RS232 , BUAD=x // x XMIT=pin // RCV=pin // FORCE_SW // UART , I/O BRGH1OK //, ENABLE=pin //, 485 DEBUGGER // CCS ICD /, B3 ;

    XMIT= RCV=, XMIT= RCV= RESTART_WDT // GETC(), GETC() WDT INVERT //(, MAX232 ,),

    UART PARITY=x //, x N,E O, (even), (odd) BITS=X // x 5~9(5~7 SCI ) FLOAT_HIGH //,.

    ,RS232_ERRORS 6 1. ERRORS // RS232_ERRORS ,

    , errors . SAMPLE_EARLY //getc(),

    . UART RETURN=pin // FLOAT_HIGH MULTI_MASTER,,

    FLOAT_HIGH XMIT , MULTI_MASTER RCV . MULTI_MASTER // RETURN ,

    ,RS232_ERRORS 6 1, PUTC RS232_ERRORS 6 .. UART .

    LONG_DATA // getc() 16 , putc 16 . 9

    DISABLE_INTS // get put ,. I/O , , UART , I/O .

    : I/O . , RS232 .#USE DELAY . ,:GETC,PUTC PRINTF. SCI ,SCI , SCI . , 3%.,.RS232_ERRORS : NO UART: Bit7 9 (get put), 9 ; Bit6 float high , 1 put ; With a UART:

  • PIC CCS C 94 ,,,,

    :13662655920 26

    Used only by get: get; Copy of RCSTA register except: Bit0 ; :#use rs232(baud=9600,xmit=PIN_A2,rcv=PIN_A3) // 9600,

    // PIN_A2 // PIN_A3

    :ex_sqw.c . #USE RTOS RTOS(the CCS Real Time Operating System)CCS,PIC,. RTOS_RUN()., ., .,,,. :#use rtos(options) options ,: timer=X // X 0~4, RTOS minor_cycle=time // time :s,ms,us,ns..

    // time . time , //.

    Statistics // , . : PIC ,..

    minor_cycle ,. project , minor_cycle .minor_cycle , RTOS minor_cycle ,

    Statistics ,,. :#use rtos(timer=0, minor_cycle=20ms) // timer0 RTOS

    // minor_cycle #TASK RTOS,,. RTOS ,#TASK RTOS .RTOS . :#TASK(options) : options ,: rate=time time :s, ms, us ns. RTOS . max=time time :s, ms, us ns. RTOS . queue=bytes RTOS , bytes 0. :, RTOS . rate RTOS(). rateminor_cycle

  • PIC CCS C 94 ,,,,

    :13662655920 27

    , minor_cycle #use rtos . max RTOS . max time queue minor_cycle .,#use rtos minor_cycle., RTOS .. queue RTOS , RTOS . queue 0. :#task(rate=1s, max=20ms, queue=5) EX_LCDKB.C : #if defined(__PCB__) #include #fuses HS,NOWDT,NOPROTECT #use delay(clock=20000000) #elif defined(__PCM__) #include #fuses HS,NOWDT,NOPROTECT,NOLVP #use delay(clock=20000000) #elif defined(__PCH__) // PCH , defined( __PCH__) 1 #include // 18F452.h #fuses HS, NOWDT, NOPROTECT, NOLVP //HS:/, NOWDT: WDT // NOPROTECT: #use delay(clock=20000000) //:delay_ms() delay_us() //#USE DELAY()#use rs232(). #endif // if #include // lcd.c #include // kbd.c void main() { char k; // k lcd_init(); // LCD kbd_init(); // lcd_putc("\fReady...\n"); //LCD , Ready... LCD , while (TRUE) { k=kbd_getc(); // if(k!=0) if(k=='*') lcd_putc('\f'); // k '*', LCD

  • PIC CCS C 94 ,,,,

    :13662655920 28

    else lcd_putc(k); // K LCD } } KBD.C : #if defined(__PCH__) // PCH , defined( __PCH__) 1 #if defined use_portb_kbd // use_portb_kbd, defined use_portb_kbd 1 #byte kbd = 0xF81 // kbd PORT B (at address 0xF81) #else #byte kbd = 0xF83 //, kbd PORT D (at address 0xF83) #endif // if #else #if defined use_portb_kbd // use_portb_kbd, defined use_portb_kbd 1 #byte kbd = 6 // kbd port B (at address 6) #else #byte kbd = 8 // kbd port D (at address 8) #endif // if #endif // if #if defined use_portb_kbd // use_portb_kbd, defined use_portb_kbd 1 #define set_tris_kbd(x) set_tris_b(x) // set_tris_kbd(x) set_tris_b(x) #else #define set_tris_kbd(x) set_tris_d(x) // set_tris_kbd(x) set_tris_d(x) #endif // if //Keypad connection: (for example column 0 is B2) // Bx: #ifdef blue_keypad // blue_keypad, #define COL0 (1

  • PIC CCS C 94 ,,,,

    :13662655920 29

    #define ROW1 (1

  • PIC CCS C 94 ,,,,

    :13662655920 30

    kchar=last_key; last_key='\0'; //('\0') } } else { if((kbd & (ALL_ROWS))!=(ALL_ROWS)) { if((kbd & ROW0)==0) row=0; else if((kbd & ROW1)==0) row=1; else if((kbd & ROW2)==0) row=2; else if((kbd & ROW3)==0) row=3; last_key =KEYS[row][col]; kbd_down = TRUE; } else { ++col; if(col==3) col=0; } } kbd_call_count=0; } set_tris_kbd(ALL_PINS); return(kchar); } LCD.C : // As defined in the following structure the pin connection is as follows: // D0 enable // D1 rs // D2 rw // D4 D4 // D5 D5 // D6 D6 // D7 D7 // // LCD pins D0-D3 are not used and PIC D3 is not used. // Un-comment the following define to use port B

  • PIC CCS C 94 ,,,,

    :13662655920 31

    // #define use_portb_lcd TRUE struct lcd_pin_map { BOOLEAN enable; // enable 1 , B0 BOOLEAN rs; // rs 1 , B1 BOOLEAN rw; // rw 1 , B2 BOOLEAN unused; // unused 1 , B3 int data : 4; // data 4 , B4,B5,B6,B7 } lcd; // lcd , PORT B #if defined(__PCH__) // PCH, #if defined use_portb_lcd #byte lcd = 0xF81 // use_portb_lcd, lcd ,PORT B (at address 0xF81) #else #byte lcd = 0xF83 // use_portb_lcd, lcd , PORT D (at address 0xF83) #endif // if #else // PCH, #if defined use_portb_lcd #byte lcd = 6 // use_portb_lcd, lcd , port B (at address 6) #else #byte lcd = 8 // use_portb_lcd, lcd , port D (at address 8) #endif // if #endif // if #if defined use_portb_lcd // use_portb_lcd, defined use_portb_lcd 1 #define set_tris_lcd(x) set_tris_b(x) // set_tris_lcd(x) set_tris_b(x) #else #define set_tris_lcd(x) set_tris_d(x) // set_tris_lcd(x) set_tris_d(x) #endif // if #define lcd_type 2 // 0=5x7, 1=5x10, 2=2 lines, lcd_type 2 #define lcd_line_two 0x40 // LCD RAM address for the second line lcd_line_two 0x40 BYTE const LCD_INIT_STRING[4] = {0x20 | (lcd_type

  • PIC CCS C 94 ,,,,

    :13662655920 32

    lcd.rw = 1; // LCD delay_cycles(1); // 1 NOP lcd.enable = 1; //LCD delay_cycles(1); // 1 NOP high = lcd.data; // 4 lcd.enable = 0; // LCD delay_cycles(1); // 1 NOP lcd.enable = 1; //LCD delay_us(1); // 1us low = lcd.data; // 4 lcd.enable = 0; // LCD set_tris_lcd(LCD_WRITE); // LCD return( (high 4); // n 4 lcd_send_nibble(n & 0xf); // n 4 } void lcd_init() { BYTE i; // i set_tris_lcd(LCD_WRITE); // LCD lcd.rs = 0; // LCD lcd.rw = 0; // LCD lcd.enable = 0; //LCD

  • PIC CCS C 94 ,,,,

    :13662655920 33

    delay_ms(15); // 15ms for(i=1;i

  • PIC CCS C 94 ,,,,

    :13662655920 34

    : . TYPEDEF ,,,.CONST ,. ,.. SHORT , I/O . SHORT SHORT . :[]. DATA DECLARATIONS()

    [type-qualifier] [type-specifier] [declarator];

    enum [id] { [ id [ = cexpr]] }

    One or more comma

    separated

    struct or

    union

    [*]

    [id]

    { [ type-qualifier [ [*]

    One or more semi-colon

    separated

    [*]id cexpr

    [ cexpr ] ]]}

    Zero or more

    typedef [type-qualifier] [type-specifier] [declarator];

    enum id , list , list id

    ., id 0, 1, list . a= cexpr

    id , id . id

    1;

    struct or union , id cexpr id , 1~8.

    [ ].. struct id struct .{ }

    . static , 0 auto ,. auto , auto double , extern , register , int1 1 int8 8 (1 ) int16 16 () int32 32 (4 )

  • PIC CCS C 94 ,,,,

    :13662655920 35

    char 8 (1 ) float 32 short int1 , 1 int int8 , 8 (1 ) long int16 , 16 () void : int a, b, c, d; //, 8 (1 ) typedef short bit; // short bit typedef int byte; // int byte bit e, f; // byte g[3][2]; // char *h; // enum boolean {false, true}; // false, true , false 0, true 1 boolean j; // byte k=5; //, byte const WEEKS=52; // WEEKS byte const FACTORS[4]={8, 16, 64, 128}; struct data_recort{

    byte a[2]; byte b:2; // byte c:3; //3 int d;

    } Typemod typemod ,, RAM, program eeprom, data eeprom . typemodname; read_function write function : void write function(int32 addr, int8 *ram, int nbytes){// addr n } : RAM , read_function write function . typemod ,,,. : void DataEE_Read(int32 addr, int8 *ram, int bytes){

    int i; for(i=0; i

  • PIC CCS C 94 ,,,,

    :13662655920 36

    } void DataEE_Write(int32 addr, int8 *ram, int bytes){

    int i; for(i=0; i

  • PIC CCS C 94 ,,,,

    :13662655920 37

    ABS() : value=abs(x) : x 8,16 32 . : . : . : . : #include : signed int target, actual;

    error=abs(target- actual); : ASSERT() : assert(condition) : condition ; : : condition, FALSE, STDERR ( USE RS232). assert( ) .#define NODEBUG, assert( )., assert( ),. : . : #include#use rs232 : assert(number_of_entries= TABLE_SIZE, RS232 : //Assertion failed,file myfile.c, line 56 : ATOF() : result=atof (string) : string ; : 32 . : .,. : . : #include : char string[10];

  • PIC CCS C 94 ,,,,

    :13662655920 38

    float x; strcpy(string,123.456); //123.456 string x=atof(string); //x 123.456 : ex_tank.c ex_tank.c : //// This example uses a trig function to calculate the volume of //// //// a fluid in a tank. The tank is in the shape of a large //// //// propane tank. The ends are half of an ellipsoid and the //// //// main body part is a horizontal cylinder. //// #if defined(__PCM__) // PCM , defined( __PCM__) 1 #include // 16F877.h #fuses HS, NOWDT, NOPROTECT, NOLVP //HS:/, NOWDT: WDT // NOPROTECT: #use delay(clock=20000000) //:delay_ms() delay_us() //#USE DELAY()#use rs232(). #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

    // 9600, // PIN_ C6 // PIN_ C7 //:GETC,PUTC PRINTF, kbhit();

    #elif defined(__PCH__) // PCH , defined( __PCH__) 1 #include // 18F452.h #fuses HS, NOWDT, NOPROTECT, NOLVP #use delay(clock=20000000) #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) // Jumpers: 8 to 11, 7 to 12 #endif // if #include // math.h #include // input.c #include // stdlib.h #define NUM_LENGTH 8 // NUM_LENGTH 8 main() { float volume, cap_vol, tube_vol, diameter, length=1, cap_length, height, offset_height, tube_length, temp1; // char string[NUM_LENGTH]; // string[] short error=FALSE; // error , while(TRUE) { do { if(error) printf("\r\nERROR MADE, PLEASE ENTER VALID NUMBERS!!"); printf("\r\n\n***NOTE: ALL DIMENSIONS ARE IN METERS***\r\n"); printf( "Enter dimensions of tank... \r\n Overall length: ");

  • PIC CCS C 94 ,,,,

    :13662655920 39

    get_string(string, NUM_LENGTH); length = atof(string); // printf( "\r\n Cap length: "); get_string(string, NUM_LENGTH); cap_length = atof(string); // printf( "\r\n Diameter of tank: "); // RS232 get_string(string, NUM_LENGTH); // RS232 diameter = atof(string); // printf("\r\n\nEnter the height of the fluid: "); // RS232 get_string(string, NUM_LENGTH); // RS232 height = atof(string); // tube_length = length - 2*cap_length; if(height>diameter||length

  • PIC CCS C 94 ,,,,

    :13662655920 40

    int lo, hi; // lo, hi hi = gethex1(); // RS232 , hi lo = gethex1(); // RS232 , lo if(lo==0xdd) return(hi); else return( hi*16+lo ); } void get_string(char* s, int max) { int len; // len char c; // c --max; // max len=0; // len do { c=getc(); // RS232 , c if(c==8) { // Backspace if(len>0) { len--; putc(c); // RS232 c putc(' '); putc(c); } } else if ((c>=' ')&&(c

  • PIC CCS C 94 ,,,,

    :13662655920 41

    char s[7]; // s[7] signed long l; // l get_string(s, 7); // RS232 7 , s l=atol(s); // s[] l return(l); } float get_float() { char s[20]; // s[7] float f; // f get_string(s, 20); // RS232 20 , s f = atof(s); // s[] f return(f); } #endif // if ATOI() ATOL() ATOI32() : ivalue=atoi(string)

    lvalue=atol(string)

    i32value=atoi32(string) : string ; : ivalue 8 .

    lvalue 16 . i32value 32 .

    : string ptr .. ,. : . : #include : char string[10]; int x; strcpy(string,123); //123.456 string x=atof(string); //x 123.456 : input.c BIT_CLEAR() : bit_clear(var, bit) : var 8 ,16 32 ();bit 0~31 ,

  • PIC CCS C 94 ,,,,

    :13662655920 42

    1 .0 ,. : : ( 0~ 7, 0~ 15, 0~ 31) 0. var&=~(1

  • PIC CCS C 94 ,,,,

    :13662655920 43

    output_b(port_b_image); // b for(i=0; i

  • PIC CCS C 94 ,,,,

    :13662655920 44

    : ex_patg.c,,. BIT_TEST() : value=bit_test(var, bit) : var 8 ,16 32 ();bit 0~31 , 1 .0 ,. : 0 1 : ( 0~ 7, 0~ 15, 0~ 31) test. , 0..((var&(1

  • PIC CCS C 94 ,,,,

    :13662655920 45

    else return 1; : CALLOC() : ptr=calloc(nmen, size) : nmen: ,; size: . : ,;, null. : calloc nmen , size , 0. : . : #include : int *iptr; iptr=calloc(5,10); //iptr 50 , 0 : CEIL() : result=ceil(value) : value ; : ; : , value .:CEIL(12.67) 13.00; : . : #include : cost=ceil(weight)*DollarsPerPound; : CLEAR_INTERRUPT() : clear_interrupt(level) : level device.h . : . : 0.., GLOBAL 0. : . : : clear_interrupt(int_timer1); // GLOBAL timer1 . : DELAY_CYCLES() : delay_cycles(count)

  • PIC CCS C 94 ,,,,

    :13662655920 46

    : count (1~255); : . : (1~255) , 4 .,, . ISR ,. : . : : delay_cycles(1); // NOP delay_cycles(25);

    // 20MHz , 5us {(1/(20*1000000)*4*25)}; : ex_cust.c,,. DELAY_MS() : delay_ms(time) : time ( 0~255)( 0~65535); : : . time .,..,. ,, . ISR,. : . : #use delay, delay_ms(time). : #use delay(clock=20000000) delay_ms(2); void delay_seconds(int n){ for(; n!=0; n--) delay_ms(1000); } : ex_sqw.c,,. DELAY_US() : delay_us(time) : time ( 0~255)( 0~65535); : : . time . INLINE ,. ,..,. ,, . ISR

  • PIC CCS C 94 ,,,,

    :13662655920 47

    ,. : . : #use delay, delay_ms(time). : #use delay(clock=20000000) do{ output_high(PIN_B0); delay_us(duty); output_low(PIN_B0); delay_us(period-duty); }while(TRUE); : ex_sqw.c,,. DISABLE_INTERRUPTS() : disable_interrupts(level) : level device.h . : : level .GLOBAL , , . ,#INT_XXX , device.h . GLOBAL (,).:,,. : PCM PCH . : #int_xxxx, disable_interrupts(level). level device.h . : disable_interrupts(GLOBAL); // disable_interrupts(INT_RDA); // RS232 enable_interrupts(ADC_DONE); //AD , GLOBAL 1 enable_interrupts(RB_CHANGE); //RB,GLOBAL 1 enable_interrupts(GLOBAL); // : ex_sisr.c ex_stwt.c,,. DIV() LDIV() : idiv=div(num, denom) ldiv=ldiv(lnum, ldenom) idiv=ldiv(lnum, ldenom) : num denom ; num , denom ; lnum ldenom ; lnum , ldenom ;

  • PIC CCS C 94 ,,,,

    :13662655920 48

    : idiv div_t , ldiv ldiv_t ; div div_t ,; ldiv ldiv_t ,;

    : div ldiv.,.,,,num()=quot()* denom()+rem();

    lnum()= quot ()* ldenom()+rem(); lnum()= quot ()* ldenom()+rem();.

    : . : #include : div_t idiv; ldiv_t lidiv; idiv=div(3, 2); //idiv quot ()=1, rem()=1; lidiv=ldiv(300, 250); //lidiv quot ()=1, rem()=50; : ENABLE_ITERRRUPTS() : enable_interrupts(level) : level device.h . : : ,.GLOBAL ,. : PCM PCH . : #int_xxxx, enable_interrupts(level). level device.h . : enable_interrupts(GLOBAL);

    enable_interrupts(INT_TIMER0); enable_interrupts(INT_TIMER1);

    : ex_sisr.c ex_stwt.c,,. ERASE_PROGRAM_EEPROM() : erase_program_eeprom(address) : address PCM 16 , PCH 32 .. : : EEPROM FLASH_ERASE_SIZE , 0xffff, FLASH_ERASE_SIZE.: FLASH_ERASE_SIZE 64, 6 . write_program_memory . : . :

  • PIC CCS C 94 ,,,,

    :13662655920 49

    : for(i=0x1000; i

  • PIC CCS C 94 ,,,,

    :13662655920 50

    //#USE DELAY()#use rs232(). #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

    // 9600, // PIN_ C6 // PIN_ C7 //:GETC,PUTC PRINTF, kbhit();

    #elif defined(__PCH__) #include #fuses HS, NOWDT, NOPROTECT, NOLVP #use delay(clock=20000000) #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) // Jumpers: 8 to 11, 7 to 12 #endif // if // global flag to send processor into sleep mode short sleep_mode; // sleep_mode // external interrupt when button pushed and released #INT_EXT // int0 void ext_isr() { static short button_pressed=FALSE; // button_pressed, FALSE if(!button_pressed) // if button action and was not pressed { button_pressed=TRUE; // button_pressed , button_pressed TRUE sleep_mode=TRUE; // activate sleep printf("The processor is now sleeping.\r\n"); ext_int_edge(L_TO_H); // L_TO_H int0 } else // if button action and was pressed { button_pressed=FALSE; // button_pressed , button_pressed FALSE sleep_mode=FALSE; // reset sleep flag ext_int_edge(H_TO_L); // H_TO_L } if(!input(PIN_B0)) // keep button action sychronized wth button flag button_pressed=TRUE; // PIN_B0 , button_pressed TRUE delay_ms(100); // debounce button } // main program that increments counter every second unless sleeping void main() { long counter; // counter sleep_mode=FALSE; // init sleep flag sleep_mode FALSE ext_int_edge(H_TO_L); // H_TO_L enable_interrupts(INT_EXT); // 1 enable_interrupts(GLOBAL); // printf("\n\n");

  • PIC CCS C 94 ,,,,

    :13662655920 51

    counter=0; // reset the counter counter 0 while(TRUE) { if(sleep_mode) // if sleep flag set 1, sleep(); // make processor sleep CPU printf("The count value is: %5ld \r\n",counter); counter++; // display count value and increment delay_ms(1000); // every second } } CPU. FABS() : result=fabs(value) : value ; : result ; : fabs ; : . : math.h ; : float result; result=fabs(-40.0); // result 40.0 : FLOOR() : result=floor(value) : value ; : result ; : floor ,.floor(12.67) 12.00; : . : math.h ; : frac=value-floor(value); // frac value, floor(value) value : FMOD() : result=fmod(val1, val2) : val1 val2 ;

  • PIC CCS C 94 ,,,,

    :13662655920 52

    : result ; : fmod val1/ val2 ,. val1-i* val2, i val1/ val2 ; val2 0 , result val1 , val2 . : . : math.h ; : float result; result=fmod(3, 2); // result 3/2 , 1; : FREE() : free(ptr) : ptr calloc, malloc realloc ; : : free ptr ,.ptr ,, ptr calloc, malloc realloc free ,. : . : stdlibm.h ; : int *iptr; iptr= malloc (10); free(iptr); : . FREXP() : result=frexp(value, &exp); : value ; exp ; : result ; GET_TIMERx() : value=get_timer0() same as: value=get_rtcc() value=get_timer1() value=get_timer2() value=get_timer3() value=get_timer4() value=get_timer5()

  • PIC CCS C 94 ,,,,

    :13662655920 53

    : : 1,3 5 16 ; 2 4 8 ; Timer0( RTCC) 8 , PIC18XXX ,Timer0(RTCC) 16 . : /.RTCC timer0 . timer . timer , 0,(254,255,0,1,2,3) : timer0. Timer1 Timer2 , PCM MCU ; Timer3 PIC18XXX MCU ;

    Timer4 PCH MCU ; Timer5 PIC18XX31 MCU ;

    : : : set_timer0(0); while(get_timer0()

  • PIC CCS C 94 ,,,,

    :13662655920 54

    #use rs232(baud=9600, ximt=pin_b3, stream=DEBUG) while(TRUE){ c=fgetc(GPS); fputc(c, HOSTPC); if(c==13) fprintf(DEBUG, Got a CR\r\n); }

    : ex_stwt.c,,. GETENV() : value=getenv(cstring); : cstring ,. : , , 0; : .., 0. FUSE_SET fuse ffff , ffff 1 FUSE_VALID fuse ffff , ffff 1 INT:iiiii iiiii , 1 ID ID(#ID ) DEVICE (PIC16C74) VERSION , VERSION_STRING , PROGRAM_MEMORY () STACK DATA_EEPROM data EEPROM READ_PROGRAM , 1 PIN:pb p b , 1 ADC_CHANNELS A/D ADC_RESOLUTION read_adc() ICD ICD , 1 SPI SPI, 1 USB USB, 1 CAN CAN, 1 I2C_SLAVE I2C slave H/W, 1 I2C_MASTER I2C master H/W, 1 PSP PSP, 1 COMP , 1 VREF , 1 LCD direct LCD H/W, 1 UART H/W UART CCPx CCPx(x ), 1 TIMERx TIMERx (x ), 1

  • PIC CCS C 94 ,,,,

    :13662655920 55

    FLASH_WRITE_SIZE FLASH FLASH_ERASE_SIZE FLASH BYTES_PER_ADDRESS BITS_PER_INSTRUCTION : : : #if getenv(VERSION)

  • PIC CCS C 94 ,,,,

    :13662655920 56

    : location ROM .,.,. : . : . : #define LOAD_REQUEST PIN_B1 #define LOADER 0x1f00

    if(input(LOAD_REQUEST) ) goto_address(LOADER)

    : setjmp.h I2C_ISR_STATE() : state=i2c_isr_state( ); : . : 8 . 0---,R/W ; 1~0x7F---;i2c_read(); 0x80---,R/W 1; i2c_write(); 0x81~0xff---; i2c_write(). : SSP , i2c , i2c ,,. : i2c . : #use i2c, i2c_isr_state( ); : #INT_SSP //SPI I2C , void i2c_isr(){

    state=i2c_isr_state(); if(state>=0x80) i2c_write( send_buffer[state-0x80] ); else if(state>0) rcv_buffer[state-1]=i2c_read();

    } : ex_slave.c ex_slave.c : #if defined(__PCM__) // PCM , defined( __PCM__) 1 #include // 16F877.h #fuses HS, NOWDT, NOPROTECT, NOLVP //HS:/, NOWDT: WDT // NOPROTECT: #use delay(clock=20000000) //:delay_ms() delay_us() //#USE DELAY()#use rs232(). #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

    // 9600, // PIN_ C6 // PIN_ C7

  • PIC CCS C 94 ,,,,

    :13662655920 57

    //:GETC,PUTC PRINTF, kbhit(); #elif defined(__PCH__) #include #fuses HS,NOWDT,NOPROTECT,NOLVP #use delay(clock=20000000) #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) // Jumpers: 8 to 11, 7 to 12 #endif // if #use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=0xa0) // SLAVE // FORCE_HW, I2C .

    // I2C_START, I2C_STOP #USE I2C . // I2C_READ, I2C_WRITE #USE I2C . // I2C_POLL #USE I2C .

    // SDA PIN_C4, SCL PIN_C3; // address 0xa0; typedef enum {NOTHING, CONTROL_READ, ADDRESS_READ, READ_COMMAND_READ} I2C_STATE; I2C_STATE fState; BYTE address, buffer[0x10]; // address, buffer[0x10] #INT_SSP //SPI I2C , void ssp_interupt () { BYTE incoming; // incoming; if (i2c_poll() == FALSE) { //, i2c_poll() FALSE , if (fState == ADDRESS_READ) { //, i2c_write (buffer[address]); //(address) fState = NOTHING; // } } else { // SSP ,i2c_poll() TRUE,

    incoming = i2c_read(); // i2c (,,) if (fState == NOTHING){ fState = CONTROL_READ; } else if (fState == CONTROL_READ) { address = incoming; // fState = ADDRESS_READ; } else if (fState == ADDRESS_READ) { buffer[address] = incoming; // address fState = NOTHING;

  • PIC CCS C 94 ,,,,

    :13662655920 58

    } } } void main () { int i; // i; fState = NOTHING; // fState address = 0x00; // address for (i=0;i

  • PIC CCS C 94 ,,,,

    :13662655920 59

    data= i2c_read(ack); : ack--- ack 1. 0 ; 1 . : data--- 8 . : I2C ., I2C ;, ., I2C_POLL ,.#USE I2C RESART_WDT,,. : i2c . : #use i2c, i2c_read( ); : i2c_start(); //i2c i2c_write(0xa1); // 0xa1 data1=i2c_read(); // data1 ,ack 1, ; data1=i2c_read(); // data2 ; i2c_stop(); //i2c ; : ex_extee.c 2416.c;,. I2C_START( ) : i2c_start( ); : . : ; : i2c ,i2c_start().,, i2c _write(). i2c_stop(),i2c_start(),.: I2 . : SCL ,SDA , I2C ; : SCL ,SDA , I2C ; ACK: SDA , SDA , ACK; NOT ACK: ,,; : . : #use i2c, i2c_start( ) ; : i2c_start(); //i2c i2c_write(0xa0); // 0xa0 i2c_write(address); //i2c i2c_start(); //i2c i2c_write(0xa1); // 0xa1

    data=i2c_read(0); // data , 0 ; i2c_stop(); //i2c ;

    : ex_extee.c 2416.c;,. I2C_STOP( ) : i2c_stop( );

  • PIC CCS C 94 ,,,,

    :13662655920 60

    : . : ; : i2c , i2c_stop( ). : SCL ,SDA , I2C ; : SCL ,SDA , I2C ; ACK: SDA , SDA , ACK; NOT ACK: ,,; : . : #use i2c, i2c_stop( ); : i2c_start(); //i2c i2c_write(0xa0); // 0xa0() i2c_write(5); //i2c 5() i2c_write(12); //i2c 12()

    i2c_stop(); //i2c ; : ex_extee.c 2416.c;,. I2C_WRITE( ) : i2c_write(data); : data 8 ; : i2c_write( ) ack ,0 ACK();1 NO ACK(); : I2C ;, i2c_write( ),; i2c_write( ) ack ;, i2c_write( );(0 ), LSB (). : I2 . : . : #use i2c, i2c_write( ); : long cmd; .

    i2c_start(); //i2c i2c_write(0xa0); // 0xa0() i2c_write(cmd); //i2c i2c_write(cmd>8); //i2c

    i2c_stop(); //i2c ; : ex_extee.c 2416.c;,. INPUT( ) : value=iput(pin); : pin ;pin device.h ,;:a(byte 5) 3 5*8+3 43,: #define PIN_A3 43; : pin , 0( FALSE); pin , 1( TRUE); : (pin).I/O #USE*_IO ;, I/O , input(),.

  • PIC CCS C 94 ,,,,

    :13662655920 61

    : . : pin device.h . : while( !input(PIN_B1) ); // B1 if( input(PIN_A0) ) printf(A0 is now high\r\n); : ex_pulse.c; ex_pulse.c : #if defined(__PCB__) // PCB , defined( __PCB__) 1 #include // 16c56.h #fuses HS, NOWDT, NOPROTECT //HS:/, NOWDT: WDT // NOPROTECT: #use delay(clock=20000000) //:delay_ms() delay_us() //#USE DELAY()#use rs232(). #use rs232(baud=9600, xmit=PIN_A3, rcv=PIN_A2)

    // 9600, // PIN_A3 // PIN_A2 //:getc(),putc() printf(), kbhit();

    #elif defined(__PCM__) #include #fuses HS, NOWDT, NOPROTECT, NOLVP #use delay(clock=20000000) #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) // Jumpers: 8 to 11, 7 to 12 #elif defined(__PCH__) #include #fuses HS, NOWDT, NOPROTECT, NOLVP #use delay(clock=20000000) #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) // Jumpers: 8 to 11, 7 to 12 #endif // if #include // ctype.h char get_scale() { char scale; // scale do {

    printf("\n\rPress S for short or L for long: "); //,, Press S for short or L for long:

    scale = getc(); // RS232 , scale scale = toupper(scale); //, scale } while ( (scale!='S') && (scale!='L') ); return(scale); // scale , get_scale() } void wait_for_low_to_high() { while(input(PIN_B1)) ; // PIN_B1 , 1,; 0, while delay_us(3); // 3us /* account for fall time */

  • PIC CCS C 94 ,,,,

    :13662655920 62

    while(!input(PIN_B1)); // PIN_B1 , 0,; 1, while } void wait_for_low() { delay_us(3); // 3us /* account for rise time */ while( input(PIN_B1) ); // PIN_B1 , 1,; 0, while } void main() { char scale; // scale BYTE time; // time do { scale = get_scale(); // RS232 ('S''L') if(scale=='S')

    setup_counters( RTCC_INTERNAL, RTCC_DIV_64 ); // Timer0

    // 64 ,TMR0 1 else

    setup_counters( RTCC_INTERNAL, RTCC_DIV_256 ); // Timer0

    // 256 ,TMR0 1 printf("\n\rWaiting...\n\r"); //,, Waiting...,, wait_for_low_to_high(); // PIN_B1 set_rtcc(0); //,(256-0)*(4y/20000000)=0.0000512y =5.12yus wait_for_low(); // PIN_B1 time = get_rtcc(); // get_rtcc()/. printf("Counter value: %2X\n\n\r", time); } while (TRUE); } PIN_B1 , RTCC 0, PIN_B1 ,/.,; INPUT_STATE() : value=iput_state(pin); : pin ;pin device.h ,;:a(byte 5) 3 5*8+3 43,: #define PIN_A3 43; : value 1 , pin ; 1,,0,. : I/O , iput( ),/; : . : pin device.h . : dir=input_state(pin_A3); // A3 , dir=1, dir=0; printf(Direction: d%,dir); : ;

  • PIC CCS C 94 ,,,,

    :13662655920 63

    INPUT_x() : value=iput_a(); value=iput_b(); value=iput_c(); value=iput_d(); value=iput_e(); value=iput_f(); value=iput_g(); value=iput_h(); value=iput_i(); value=iput_k(); : ; : value 8 ,; : .#USE*_IO. , I/O, input_x(),. : data=input_b( ); // B : ex_psp.c; ex_psp.c : //// Configure the CCS prototype card as follows: //// //// Connect pin 23 to 27 and pin 21 to 28. //// //// Connect one end of the parrallel printer cable to the PC. //// //// Also make the following connections: //// //// IBM parallel //// //// cable pin Protoboard //// //// 1 22 E1 //// //// 2 32 D0 //// //// 3 33 D1 //// //// 4 34 D2 //// //// 5 35 D3 //// //// 6 36 D4 //// //// 7 37 D5 //// //// 8 38 D6 //// //// 9 39 D7 //// //// 11 1 C0 //// //// 12 27 gnd //// //// 13 28 +5V //// //// 15 28 +5V //// //// 18 27 gnd //// #if defined(__PCM__) // PCM , defined( __PCM__) 1 #include // 16F877.h #fuses HS, NOWDT, NOPROTECT, NOLVP //HS:/, NOWDT: WDT

  • PIC CCS C 94 ,,,,

    :13662655920 64

    // NOPROTECT: #use delay(clock=20000000) //:delay_ms() delay_us() //#USE DELAY()#use rs232(). #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

    // 9600, // PIN_C6 // PIN_C7 //:getc(),putc() printf(), kbhit();

    #elif defined(__PCH__) #include #fuses HS, NOWDT, NOPROTECT, NOLVP #use delay(clock=20000000) #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) // Jumpers: 8 to 11, 7 to 12 #error For PIC18 change read/write _bank to use a buffer #endif // if #define BUSY_LINE PIN_C0 // BUSY_LINE PIN_C0 #define BUFFER_SIZE 96 // BUFFER_SIZE 96 int next_in = 0; // next_in, 0; int next_out = 0; // next_out, 0; short data_lost = TRUE; //() data_lost, TRUE; #int_psp // PSP , void psp_isr() { if( psp_overflow( ) ) // CS=0,WR=0, PSP ,, PSP, IBOV 1,;

    // CS=0,RD=0, PSP ,,PSP , IBOV 1,;

    data_lost=TRUE; if( psp_input_full( ) ) { // CS=0 WR=0, PSP , CS=1 WR=1 , IBF=1, PSPIF=1 write_bank( 2, next_in++, input_D( ) ); // PSP ( D), bank2 if( next_in == BUFFER_SIZE ) next_in = 0; if( next_in == next_out ) output_high( BUSY_LINE ); // PIN_C0 , PSP } } void main( ) { setup_adc_ports( NO_ANALOGS ); // setup_psp( PSP_ENABLED ); // PSP enable_interrupts( GLOBAL ); // enable_interrupts( INT_PSP ); //PSP output_low( BUSY_LINE ); // PIN_C0 , PSP printf( "Waiting for print data... \r\n\n" ); // RS232

  • PIC CCS C 94 ,,,,

    :13662655920 65

    while( true ) { if( next_in!=next_out ) { putc( read_bank( 2,next_out) );

    // bank2 , next_out , RS232 , if( ++next_out==BUFFER_SIZE ) next_out=0; if(data_lost) { printf( "\r\nData Lost!!!\r\n" ); data_lost = FALSE; } output_low( BUSY_LINE ); // PIN_C0 , PSP } } } PSP , bank2, bank2 , RS232; next_in 96 , 0, next_out 96 , 0; ISALNUM(char) ISALPHA(char) ISDIGIT(char) ISLOWER(char) ISSPACE(char) ISUPPER(char) ISXDIGIT(char) ISCNTRL(x) ISGRAPH(x) ISPRINT(x) ISPUNCT(x) : value=isalnum(datac); value=isalpha(datac); value=isdigit(datac); value=islower(datac); value=isspace(datac); value=isupper(datac); value=isxdigit(datac); iscntrl(x); x space ; isgraph(x); x space ; isprint(x); x space space ; ispunct(x); x space ,,; : datac 8 ;

  • PIC CCS C 94 ,,,,

    :13662655920 66

    : datac , value 0( FALSE); datac , value 1( TRUE); : ,: isalnum(x); x 0~9, A ~ Z a ~ z , isalnum(x) 1; isalpha(x); x A ~ Z a ~ z , isalpha(x) 1; isdigit(x); x 0~9 , isdigit(x) 1; islower(x); x a ~ z , islower(x) 1; isupper(x); x A ~ Z, isupper(x) 1;

    isspace(x); x space , isspace(x) 1; isxdigit(x); x 0 ~ 9, A ~ F a ~ f , isalnum(x) 1;

    : . : ctype.h ,; : char id[20]; // id[20] .. if( isalpha(id[0]) ) { valid_id=TRUE; // id[0]A ~ Z a ~ z , valid_id TRUE for(i=1; i

  • PIC CCS C 94 ,,,,

    :13662655920 67

    ITOA() : string=itoa(i32value, i8base,string); : i32value 32 ; i8base 8 ; string . : string . : 32 i32value, i8base , string;,;,0. : . : #include, stdlib.h, itoa(); : int32 x=1234; // x 32 , 0x1234 char string[5]; // string[], 5 ; itoa(x, 10, string); // x 10 , string //string 1234. : KBHIT( ) : value=kbhit( ); value=kbhit( stream ); : stream id RS232 ; stream, getc() stream, getc(); : getc(), kbhit( ) 0( FALSE); getc(), kbhit( ) 1(TRUE); : RS232 RCV ,RS232 , kbhit( ) TRUE; , getc(),RS232 kbhit( )TRUE; kbhit( ),.: RS232 , kbhit( ) 10 ,. : . : #use rs232 , kbhit( ); : char timed_getc(){

    long timeout; // timeout timeout_error=FALSE; // timeout_error FALSE timeout=0; while(!kbhit( ) && (++timeout

  • PIC CCS C 94 ,,,,

    :13662655920 68

    return(0); }

    } : ex_tgetc.c; LABEL_ADDRESS() : value=label_address( label); : label, C (C ). : PCB PCM , 16 ; PCH , 32 . : ROM .,,. : . : . : start: // start a=(b+c)

  • PIC CCS C 94 ,,,,

    :13662655920 69

    segment data area(), offset 0~15. LCD_symbol( ) LCD segment memory(LCD ),,LCD . : LCD CPU ; : devices.h . : lcd_load( buffer, 0, 16 ); : ex_92lcd.c; ex_92lcd.c : #if defined( __PCM__ ) // PCM , defined( __PCM__) 1 #include // 16c924.h #fuses HS, NOWDT, PUT //HS:/, NOWDT: WDT // NOPROTECT: #use delay(clock=20000000) //:delay_ms() delay_us() //#USE DELAY()#use rs232(). #endif // if

    // LCD Configuration(LCD ) //

    // Digit segments() A B C D E F G DP

    // b7 b6 b5 b4 b3 b2 b1 b0

    #define DIGIT4 COM1+26, COM1+17, COM2+17, COM3+26, COM2+25, COM1+25, COM2+26, COM3+17

    #define DIGIT3 COM1+24, COM1+16, COM2+16, COM3+24, COM2+23, COM1+23, COM2+24, COM3+16

    #define DIGIT2 COM1+22, COM1+19, COM2+19, COM3+22, COM2+21, COM1+21, COM2+22, COM3+19

    #define DIGIT1 COM1+20, COM1+18, COM2+18, COM3+20, COM2+28, COM1+28, COM2+20, COM3+18 // character 0 1 2 3 4 5 6 7 8 9 byte const Digit_Map[10] = {0xFC,0x60,0xDA,0xF2,0x66,0xB6,0xBE,0xE0,0xFE,0xE6}; #define BLANK 0 // BLANK 0 #define DASH 11 // DASH 11 byte lcd_pos; // lcd_pos void lcd_putc(char c) { byte segments; // segments if(c=='\f') // '\f' lcd_pos=0; // c '\f'(), lcd_pos 0; else { if( (c>='0') && (c

  • PIC CCS C 94 ,,,,

    :13662655920 70

    lcd_pos++; } void main() { long number = 0; // number, 0 setup_lcd( LCD_MUX14|STOP_ON_SLEEP, 2, ALL_LCD_PINS ); while(TRUE) { printf(lcd_putc,"\f%4lu",number); if(number++==10000) number=0; delay_ms(100); // 100ms } } LCD_SYMBOL( ) : lcd_ symbol (symbol, b7_addr, b6_addr, b5_addr, b4_addr, b3_addr, b2_addr, b1_addr, b0_addr); : symbol 8 ; b7_addr , LCD , symbol 7 ; b6_addr , LCD , symbol 6 ; b5_addr , LCD , symbol 5 ; b4_addr , LCD , symbol 4 ; b3_addr , LCD , symbol 3 ; b2_addr , LCD , symbol 2 ; b1_addr , LCD , symbol 1 ; b0_addr , LCD , symbol 0 ; : ; : 8LCD segment data area(). symbol 7 1, b7_addr 1,, b7_addr; symbol b7_addr ; b7_addr , LCD RAM . : LCD CPU ; : devices.h . : #define DIGIT_1_CONFIG

    COM0+2,COM0+4,COM0+5,COM2+4,COM2+1,COM1+4,COM1+5 for(i=1; I

  • PIC CCS C 94 ,,,,

    :13662655920 71

    SETUP_LCD( ) : setup_lcd (mode, prescale, [segments]); : mode devices.h : LCD_DISABLED, LCD_STATIC, LCD_MUX12, LCD_MUX13, LCD_MUX14

    :STOP_ON_SLEEP, USE_TIMER_1 LCD_DISABLED, LCD_STATIC, LCD_MUX12, LCD_MUX13, LCD_MUX14( | ); devices.h .

    Prescale 0~15, LCD clock segment, LCD clock segment ,( | ); LCD clock segment : SEGO_4, SEG5_8, SEG9_11, SEG12_15, SEG16_19, SEGO_28, SEG29_31, ALL_LCD_PINS; segments, segments. : ; : 923 LCD 924 LCD ; : LCD CPU ; : devices.h . : setup_lcd( LCD_MUX14 | STOP_ON_SLEEP, 2 ); : ex_92lcd.c; ex_92lcd.c ,. LDEXP( ) : result=ldexp (value, exp); : value ; exp ; : result value ,, value 2 exp . : ldexp( ) 2 (). : CPU . : math.h; : float result; signed int exp; result=ldexp( 0.5, 0 ); // result 0.5 : ; LOG( ) : result=log (value); : value ; : result ,; : x ( ln x); x 0, x ,; : error ; errno.h , errno . errno ,, perror .

  • PIC CCS C 94 ,,,,

    :13662655920 72

    : log( x ); x ; : CPU . : math.h; : lnx=log( x ); // e ,x , x ; : ; LOG10( ) : result=log10 (value); : value ; : result ,; : x 10 ( log x); x 0, x ,; : error ; errno.h , errno . errno ,, perror . : log10 ( x ); x ; : CPU . : math.h; : db=log10 ( read_adc()*(5.0/255) )*10; // 10 ,x ; : ; LONGJMP( ) : longjmp ( env, val ); : env: longjmp( ); val: setjmp , val 0, setjmp 1; : longjmp( ),, setjmp val; : ; : CPU . : #include : longjmp( jmpbuf, 1 ); : ; MAKE8( ) : i8=make8( var, offset); : var 16 32 ; offset , 1,2 3;

  • PIC CCS C 94 ,,,,

    :13662655920 73

    : 8 ; : var , offset ,;, i8=( ( var>>(offset*8) ) &0xff ); : CPU . : : int32 x; int y; y=make8(x, 3); // x 8 ( x 3 ) y; : ; MAKE16( ) : i16=make16( varhigh, varlow); : varhigh varlow 8 ; : 16 ; : 16 ; 16, 32 , 16 /32 , LSB( 8 ), , i16=(int16)( ( varhigh &xff)*0x100 )+( varlow&0xff ); : CPU . : ; : long x; int hi, lo; x=make16(hi, lo); // hi=0x07, lo=0x02, x=0x0702; : ltc1298.C; ltc1298.c : #ifndef ADC_CS // ADC_CS,; #define ADC_CLK PIN_B0 // ADC_CLK PIN_B0; #define ADC_DOUT PIN_B1 // ADC_DOUT PIN_B1; #define ADC_DIN PIN_B2 // ADC_DIN PIN_B2; #define ADC_CS PIN_B3 // ADC_CS PIN_B3; #endif // if ; void adc_init() { output_high(ADC_CS); // ADC_CS , SPI ;; } /*, SPI ,:data,, number_of_bits */ void write_adc_byte(BYTE data_byte, BYTE number_of_bits) { BYTE i; // i; delay_us(2); // 2us; for(i=0; i

  • PIC CCS C 94 ,,,,

    :13662655920 74

    else output_high(ADC_DIN); // ADC_DIN ; data_byte=data_byte>>1; // data_byte output_high(ADC_CLK); // ADC_CLK ; delay_us(50); // 50us; output_low(ADC_CLK); // ADC_CLK ; delay_us(50); // 50us; } } /*, SPI , number_of_bits */ BYTE read_adc_byte(BYTE number_of_bits) { BYTE i, data; // i, data; data=0; // data for(i=0;i

  • PIC CCS C 94 ,,,,

    :13662655920 75

    } void convert_to_volts( long int data, char volts[6]) { BYTE i; // i; long int temp,div; // temp, div; div=0x3330; // div for(i=0;i

  • PIC CCS C 94 ,,,,

    :13662655920 76

    //:delay_ms() delay_us() //#USE DELAY()#use rs232(). #use rs232(baud=9600, xmit=PIN_c6, rcv=PIN_c7)

    // 9600, // PIN_ C6 // PIN_ C7 //:GETC,PUTC PRINTF, kbhit();

    #bit t1_overflow=0x0C.0 //#bit timer1 ; // #bit t1_overflow=0xF9E.0 (PIC18, Reminder) void main() { int cycles8, cycles; int32 freq; // 32 long freqc_high; // freqc_high long freqc_low; // freqc_low while (TRUE) { cycles8=0; cycles=0; freqc_high=0; t1_overflow=0; //timer1 0; set_timer1(0); // timer1 0;

    setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1); // 1,

    // //,,T1

    /* ___ wait one second ___ 1 */ while (cycles!=0xFF) { //true=3, false=4 cycles8=0; //1 cycle //start inner loop while (cycles8!=0xFF) { //true=3, false=4 if (t1_overflow) //true=2,false=3 //----| {t1_overflow=0;freqc_high++;} //6 cycles // | else // |-- 8 cycles {delay_cycles(5);} //----| delay_cycles(62); //x cycles8++; //1 ///2 cycles to jump to top //math: end inner loop //math: total inner loop=((3+8+x+1+2)*255 + 4)*255 //math: if x=62.87781 then inner loops takes 5mil instructions //math: if x=62 then inner loop takes 4942920, have to fill 57080 cycles }

  • PIC CCS C 94 ,,,,

    :13662655920 77

    delay_cycles(216); //y cycles++; ///1 cycle ///2 cylces to jump to top //math: outer=(3+1+y+1+2)*255+4=57080 //math: y=(57080-4)/255)-(3+1+0+0+1+2) //math: if y=216.827450980392156862745098039216 then outer loop cylces is 57080 //math: if y=216 then outer loop cycles is off by 211 cycles. z=211 } delay_cycles(211); //z /* ___ end waiting 1 second ___ 1 */ setup_timer_1(T1_DISABLED); // timer1 if (t1_overflow) //check one last time for overflow freqc_high++; freqc_low=get_timer1(); //get timer1 value as the least sign. 16bits of freq counter freq=make32(freqc_high,freqc_low); //use new make32 function to join lsb and msb printf("%LU Hz\r\n",freq); //and print frequency } } //, 1 ,; MALLOC( ) : ptr=malloc(size) : size ,; : ,;, ; : , size,; : CPU . : STDLIBM.H,: #include ; : int *iptr; iptr=malloc(10); //iptr 10 ; : ; MEMCPY( ) MEMMOVE( ) : memcpy(destination, source, n); memmove(destination, source, n); : destination ; source ; n ; : ;

  • PIC CCS C 94 ,,,,

    :13662655920 78

    : RAM n RAM ;,,,& memmove copy(),: n , n ,; : CPU . : ; : memcpy( &structA, &structB, sizeof (structA) ); memcpy( arrayA, arrayB, sizeof (arrayA) ); memcpy(&structA, &databyte, 1); char a[20]= "hello"; memmove(a, a+2, 5); //a "llo" : ; MEMSET( ) : memset(destination, value, n); : destination ; value 8 ; n 8 ; : ; : n value destination ; ,,,&; : CPU . : ; : memset(arrayA, 0, sizeof (arrayA) ); // arrayA 0; memset(arrayB, '?', sizeof (arrayB) ); // arrayB '?'; memset(&structA, 0xff, sizeof (structA) ); // structA 0xff; : ; MODF( ) : result=modf( value, &integral ); : value integral ; : ; : value ,,value ; integral , value ; : CPU . : math.h, #include : float result, integral; result=modf(123.987, &integral); // result .987, integral 123.0000 : ;

  • PIC CCS C 94 ,,,,

    :13662655920 79

    MUL( ) : prod=_mul(val1, val2); : val1 val2 8 16 ; : val1 val2 8 , 16 ; val1 val2 16 , 32 ; : ; : CPU . : ; : int a=50, b=100; long int c: c=_mul(a, b); //c 5000 : ; OFFSETOF( ) OFFSETOFBIT( ) : value=offsetof( stype, field ); value=offsetofbit( stype, field ); : stype ; field ; : 8 ; : ;offsetof( );offsetofbit( ); : CPU . : stddef.h, #include < stddef.h > : struct time_structure{ int hour, min, sec; //,, 8 int zone : 4; //,, 4 short daylight_savings; //,, short ; } x=offsetof( time_structure, sec); //x 2 x=offsetofbit( time_structure, sec); // x 16 x=offsetof( time_structure, daylight_savings ); // x 3 x=offsetofbit( time_structure, daylight_savings ); // x 28 : ;

  • PIC CCS C 94 ,,,,

    :13662655920 80

    OUTPUT_A( ) OUTPUT_B( ) OUTPUT_C( ) OUTPUT_D( ) OUTPUT_E( ) OUTPUT_F( ) OUTPUT_G( ) OUTPUT_H( ) OUTPUT_J( ) OUTPUT_K( ) : output_a( value ) output_b( value ) output_c( value ) output_d( value ) output_e( value ) output_f( value ) output_g( value ) output_h( value ) output_j( value ) output_k( value ) : value 8 ; : ; : ,#USE *_IO : CPU A E : ; : output_b0xf0// B 0xf0 : ex_patg.c; : ex_patg.c : #if defined(__PCM__) // PCM , defined( __PCM__) 1 #include // 16F877.h #fuses HS, NOWDT, NOPROTECT, NOLVP //HS:/, NOWDT: WDT // NOPROTECT: #use delay(clock=20000000) //:delay_ms() delay_us() //#USE DELAY()#use rs232(). #elif defined(__PCH__) #include #fuses HS, NOWDT, NOPROTECT, NOLVP #use delay(clock=20000000) #endif // if #define NUM_OUTPUTS 7 // NUM_OUTPUTS 7 //NOTE: periods MUST be multiples of 400 : 400 //Periods are in microseconds (us)

  • PIC CCS C 94 ,,,,

    :13662655920 81

    #define PERIOD_0 400 // PERIOD_0 400 #define PERIOD_1 800 // PERIOD_1 800 #define PERIOD_2 1600 // PERIOD_2 1600 #define PERIOD_3 2000 // PERIOD_3 2000 #define PERIOD_4 20000 // PERIOD_4 20000 #define PERIOD_5 64000 // PERIOD_5 64000 #define PERIOD_6 2000000 // PERIOD_6 2000000 const long wave_period[NUM_OUTPUTS] = { PERIOD_0/400, PERIOD_1/400, PERIOD_2/400, PERIOD_3/400, PERIOD_4/400, PERIOD_5/400, PERIOD_6/400}; // wave_period[7] long counter[NUM_OUTPUTS] = {0,0,0,0,0,0,0}; // counter[7] int port_b_image; // port_b_image // This interrupt is used to output the waveforms. // The interrupt is automatically called ever 200us. 200us #INT_TIMER1 // timer1 void wave_timer() { int i; // i set_timer1(0xFC4F); // sets timer to interrupt in 200us(:65535-200/(4/20)=0xfc17 ) output_b(port_b_image); // b for(i=0; i

  • PIC CCS C 94 ,,,,

    :13662655920 82

    , B3 2000us , B4 20000us , B5 64000us , B6 2000000us . OUTPUT_BIT( ) : output_bit( pin, value ); : pin devices.h CPU ,;:a 3, 5*8+3 43,: #define PIN_A3 43

    value 1 0; : ; : I/O(01),#USE *_IO ; : CPU ; : Pin devices.h ; : output_bit( PIN_B0, 0 ); // output_low(PIN_B0); output_bit(PIN_B0, input(PIN_B1) ); // PIN_B1 PIN_B0 output_bit(PIN_B0, shift_left( &data,1, input(PIN_B1) ) );

    // data (MSB) PIN_B0 , // PIN_B1 data (LSB);

    : #use fixed_io(a_outputs=PIN_A2,PIN_A3) // PINA2 PIN_A3 ; #use fast_io(A) // set_tris_A(0x00) I/O ; // A

    : ex_extee.c 9356.c; : ex_extee.c : #if defined(__PCB__) // PCB , defined( __PCB__) 1 #include // 16c56.h #fuses HS, NOWDT, NOPROTECT //HS:/, NOWDT: WDT // NOPROTECT: #use delay(clock=20000000) //:delay_ms() delay_us() //#USE DELAY()#use rs232(). #use rs232(baud=9600, xmit=PIN_A3, rcv=PIN_A2)

    // 9600, // PIN_A3 // PIN_A2 //:GETC,PUTC PRINTF, kbhit();

    #elif defined(__PCM__) #include #fuses HS,NOWDT,NOPROTECT,NOLVP #use delay(clock=20000000) #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) // Jumpers: 8 to 11, 7 to 12 #elif defined(__PCH__) #include

  • PIC CCS C 94 ,,,,

    :13662655920 83

    #fuses HS,NOWDT,NOPROTECT,NOLVP #use delay(clock=20000000) #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) // Jumpers: 8 to 11, 7 to 12 #endif // if #include // input.c #include // 2416.c void main() { BYTE value, cmd; // value, cmd EEPROM_ADDRESS address; // EEPROM_ADDRESS long int, 16 init_ext_eeprom(); // eeprom I/O do { do { printf("\r\nRead or Write: "); cmd=getc(); // RS232 cmd=toupper(cmd); // cmd cmd putc(cmd); } while ( (cmd!='R') && (cmd!='W') ); // R W

    printf("\n\rLocation: ");

    #if sizeof(EEPROM_ADDRESS)==1 // EEPROM_ADDRESS 1 , sizeof(EEPROM_ADDRESS)==1 1 address = gethex(); #else // EEPROM_ADDRESS 1 #if EEPROM_SIZE>0xfff address = gethex(); #else // EEPROM_SIZE 0xfff address = gethex1(); // RS232 , eeprom #endif // if address = (address

  • PIC CCS C 94 ,,,,

    :13662655920 84

    BYTE gethex1() { char digit; // digit digit = getc(); // RS232 putc(digit); // RS232 if(digit0) { len--; putc(c); // RS232 c putc(' '); putc(c); } } else if ((c>=' ')&&(c

  • PIC CCS C 94 ,,,,

    :13662655920 85

    // in the following functions #ifdef _STDLIB //_STDLIB, signed int get_int() { char s[5]; // s[5] signed int i; // i get_string(s, 5); // RS232 5 , s i=atoi(s); // s[] i return(i); } signed long get_long() { char s[7]; // s[7] signed long l; // l get_string(s, 7); // RS232 7 , s l=atol(s); // s[] l return(l); } float get_float() { char s[20]; // s[7] float f; // l get_string(s, 20); // RS232 20 , s f = atof(s); // s[] f return(f); } #endif // if : 2416.c : //// Library for a MicroChip 24LC16B //// //// init_ext_eeprom(); Call before the other functions are used //// //// write_ext_eeprom(a, d); Write the byte d to the address a //// //// d = read_ext_eeprom(a); Read the byte d from the address a //// //// b = ext_eeprom_ready(); Returns TRUE if the eeprom is ready //// //// to receive opcodes //// //// The main program may define EEPROM_SDA //// //// and EEPROM_SCL to override the defaults below. //// //// Pin Layout //// //// ----------------------------------------------------------- //// //// | | //// //// | 1: NC Not Connected | 8: VCC +5V | //// //// | 2: NC Not Connected | 7: WP GND | //// //// | 3: NC Not Connected | 6: SCL EEPROM_SCL and Pull-Up | //// //// | 4: VSS GND | 5: SDA EEPROM_SDA and Pull-Up | //// //// ----------------------------------------------------------- ////

  • PIC CCS C 94 ,,,,

    :13662655920 86

    #ifndef EEPROM_SDA // EEPROM_SDA, #define EEPROM_SDA PIN_C4 // EEPROM_SDA PIN_C4 #define EEPROM_SCL PIN_C3 // EEPROM_SCL PIN_C3 #endif // if #use i2c(master, sda=EEPROM_SDA, scl=EEPROM_SCL) // master // FORCE_HW, I2C .

    // I2C_START, I2C_STOP #USE I2C . // I2C_READ, I2C_WRITE #USE I2C . // I2C_POLL #USE I2C .

    // sda EEPROM_SDA, scl EEPROM_SCL #define EEPROM_ADDRESS long int // EEPROM_ADDRESS long int #define EEPROM_SIZE 2048 // EEPROM_SIZE 2048 void init_ext_eeprom() { output_float(EEPROM_SCL); // EEPROM_SCL , output_float(EEPROM_SDA); // EEPROM_SDA , } BOOLEAN ext_eeprom_ready() { int1 ack; // ack i2c_start(); // ack = i2c_write(0xa0); // 0xa0; ack=0,(ACK);

    // ack=1,(NO ACK); i2c_stop(); // return !ack; } // ext_eeprom_ready(), 1,; 0, eeprom void write_ext_eeprom(long int address, BYTE data) { while(!ext_eeprom_ready()); //, i2c_start(); // i2c_write( (0xa0|(BYTE)(address>>7))&0xfe); //() i2c_write(address); // i2c_write(data); // i2c_stop(); // } BYTE read_ext_eeprom(long int address) { BYTE data; // data while(!ext_eeprom_ready()); //,, i2c_start(); //: i2c_write( (0xa0|(BYTE)(address>>7))&0xfe); //() i2c_write(address); // i2c_start(); // i2c_write( (0xa0|(BYTE)(address>>7))|1); //()

  • PIC CCS C 94 ,,,,

    :13662655920 87

    data=i2c_read(0); // I2C , ack=0() i2c_stop(); // return(data); // I2C } 24C16, PC RS232 : 9356.c : #ifndef EEPROM_SELECT // EEPROM_SELECT, #define EEPROM_SELECT PIN_B4 // EEPROM_SELECT PIN_B4 #define EEPROM_CLK PIN_B2 // EEPROM_CLK PIN_B2 #define EEPROM_DI PIN_B1 // EEPROM_DI PIN_B1 #define EEPROM_DO PIN_B0 // EEPROM_DO PIN_B0 #endif // if #define EEPROM_ADDRESS BYTE // EEPROM_ADDRESS BYTE #define EEPROM_SIZE 256 // EEPROM_SIZE 256 void init_ext_eeprom() { BYTE cmd[2]; // cmd[2] BYTE i; // i output_low(EEPROM_DI); // EEPROM_DI output_low(EEPROM_CLK); // EEPROM_CLK output_low(EEPROM_SELECT); // EEPROM_SELECT cmd[0]=0x80; // cmd[1]=0x9; // for(i=1;i

  • PIC CCS C 94 ,,,,

    :13662655920 88

    cmd[0]=data; // cmd[1]=address; // cmd[2]=0xa; // for(i=1;i

  • PIC CCS C 94 ,,,,

    :13662655920 89

    OUTPUT_FLOAT() : output_float( pin ); : pin devices.h CPU ,;:a 3, 5*8+3 43,: #define PIN_A3 43; : ; : I/O , I/O ,,; : CPU ; : Pin devices.h ; : if( ( data&0x80 )==0 ) output_low(PIN_A0); // PIN_A0 ; else output_float( PIN_A0 ); // PIN_A0 ,; : ; OUTPUT_HIGH( ) : output_high( pin ); : pin devices.h CPU ,;:a 3, 5*8+3 43,: #define PIN_A3 43; : ; : I/O ,#USE *_IO ; : CPU ; : Pin devices.h ; : output_high( PIN_A0 ); // PIN_A0 1 ; //#USE *_IO , I/O // : ex_sqw.c : ex_sqw.c : #if defined(__PCM__) // PCM , defined( __PCM__) 1 #include // 16F877.h #fuses HS,NOWDT,NOPROTECT //HS:/, NOWDT: WDT // NOPROTECT: #use delay(clock=20000000) //:delay_ms() delay_us() //#USE DELAY()#use rs232(). #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

    // 9600, // PIN_C6 // PIN_C7 //:GETC,PUTC PRINTF, kbhit();

    #elif defined(__PCB__) #include #fuses HS,NOWDT,NOPROTECT,NOLVP

  • PIC CCS C 94 ,,,,

    :13662655920 90

    #use delay(clock=20000000) #use rs232(baud=9600, xmit=PIN_A3, rcv=PIN_A2) // Jumpers: 11 to 17, 12 to 18 #elif defined(__PCH__) #include #fuses HS,NOWDT,NOPROTECT,NOLVP #use delay(clock=20000000) #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) // Jumpers: 8 to 11, 7 to 12 #endif // if void main() { printf("Press any key to begin\n\r"); getc(); printf("1 khz signal activated\n\r"); while (TRUE) { output_high(PIN_B0); //(PIN_B0) delay_us(500); // 500us output_low(PIN_B0); //(PIN_B0) delay_us(500); // 500us } } delay_us(), output_high()