tài liệu CCS: C Compiler Reference Manual
C Compiler Reference Manual June 2010 This manual documents software version Review the readme.txt file in the product directory for changes made since this version Copyright © 1994, 2010 Custom Computer Services, Inc All rights reserved worldwide No part of this work may be reproduced or copied in any form or by any means- electronic, graphic, or mechanical, including photocopying, recording, taping, or information retrieval systems without prior permission Table Of Contents Overview PCB, PCM and PCH Overview Installation Technical Support Directories File Formats Invoking the Command Line Compiler .5 PCW Overview Program Syntax 17 Overall Structure 17 Comment 17 Trigraph Sequences 19 Multiple Project Files 19 Multiple Compilation Units 20 Example 30 Statements 31 Statements 31 if 32 while 32 .33 do-while .33 for .33 switch 34 return 34 goto 35 label 35 break 35 continue 36 expr 36 ; 36 stmt 36 Expressions 37 Expressions 37 Operators 38 Operator Precedence .39 Reference Parameters 40 Variable Argument Lists 40 Default Parameters 41 Overloaded Functions 42 Data Definitions 43 Basic and Special types 43 Declarations 46 Non-RAM Data Definitions .46 Using Program Memory for Data 48 Function Definition 50 v C Compiler Reference Manual June 2010 Functional Overviews 51 I2C 51 ADC 52 Analog Comparator 53 CAN Bus 54 CCP1 56 CCP2, CCP3, CCP4, CCP5, CCP6 56 Configuration Memory .57 DAC 58 Data Eeprom .59 External Memory .60 General Purpose I/O 61 Internal LCD 62 Internal Oscillator .63 Interrupts 64 Linker .65 Low Voltage Detect 69 PMP 70 Power PWM .71 Program Eeprom 72 PSP 74 RS232 I/O 75 RTOS 77 SPI 79 Timer0 80 Timer1 81 Timer2 82 Timer3 82 Timer4 83 Timer5 83 USB 84 Voltage Reference 86 WDT or Watch Dog Timer 87 Pre-Processor Directives 89 PRE-PROCESSOR 89 #ASM #ENDASM .91 #BIT 94 #BUILD 95 #BYTE 96 #CASE 97 _DATE_ 97 #DEFINE 98 #DEVICE 99 #DEFINEDINC 101 _DEVICE_ 101 #ERROR 102 #EXPORT (options) .102 FILE 104 FILENAME .104 vi Table Of Contents #FILL_ROM 104 #FUSES 105 #HEXCOMMENT 106 #ID 106 #IF exp #ELSE #ELIF #ENDIF 107 #IFDEF #IFNDEF #ELSE #ELIF #ENDIF .108 #IGNORE_WARNINGS 109 #IMPORT (options) .109 #INCLUDE 111 #INLINE 111 #INT_xxxx 112 #INT_DEFAULT .115 #INT_GLOBAL .116 LINE .116 #LIST 117 #LINE .117 #LOCATE .118 #MODULE 118 #NOLIST 119 #OPT 120 #ORG .120 #OCS 122 PCB 122 PCM 123 PCH 123 #PIN_SELECT 124 #PRAGMA 125 #PRIORITY .126 #RESERVE 126 #ROM .127 #SEPARATE 128 #SERIALIZE 128 #TASK 130 TIME .131 #TYPE 131 #UNDEF 132 #USE DELAY 133 #USE DYNAMIC_MEMORY 134 #USE FAST_IO 135 #USE FIXED_IO .135 #USE I2C 136 #USE RS232 137 #USE RTOS 140 #USE SPI .141 #USE STANDARD_IO 143 #USE TOUCHPAD 143 #WARNING 144 #WORD 145 #ZERO_RAM 145 vii C Compiler Reference Manual June 2010 Built-in-Functions 147 BUILT-IN-FUNCTIONS 147 abs( ) 151 adc_done( ) 151 assert( ) 152 atoe( ) .152 atof( ) 153 atoi( ) atol( ) atoi32( ) 154 bit_clear( ) .155 bit_set( ) .155 bit_test( ) .156 brownout_enable( ) .156 bsearch( ) 157 calloc( ) 158 ceil( ) 158 clear_interrupt( ) 159 dac_write( ) 159 delay_cycles( ) 160 delay_ms( ) 160 delay_us( ) .161 disable_interrupts( ) .162 div( ) ldiv( ) 163 enable_interrupts( ) 164 erase_eeprom( ) 165 erase_program_eeprom( ) 165 exp( ) 166 ext_int_edge( ) 166 fabs( ) .167 floor( ) 167 fmod( ) 168 free( ) 168 frexp( ) 169 get_timerx( ) 169 get_tris_x( ) 170 getc( ) getch( ) getchar( ) fgetc( ) 171 getenv( ) 172 gets( ) fgets( ) 174 goto_address( ) .174 i2c_isr_state( ) .175 i2c_poll( ) .176 i2c_read( ) 177 i2c_slaveaddr( ) 177 i2c_start( ) .178 i2c_stop( ) 179 i2c_write( ) .179 i2c_speed( ) 180 input( ) 181 input_change_x( ) 182 viii Table Of Contents input_state( ) 182 input_x( ) .183 interrupt_active( ) 184 isalnum(char) isalpha(char) isdigit(char) islower(char) isspace(char) isupper(char) isxdigit(char) iscntrl(x) isgraph(x) isprint(x) ispunct(x) 184 isamong( ) .186 itoa( ) 186 jump_to_isr 187 kbhit( ) 188 label_address( ) 189 labs( ) .189 lcd_load( ) 190 lcd_contrast( ) .190 lcd_symbol( ) .191 ldexp( ) 192 log( ) .192 log10( ) 193 longjmp( ) 193 make8( ) 194 make16( ) .194 make32( ) .195 malloc( ) 195 memcpy( ) memmove( ) 196 memset( ) .197 modf( ) 197 _mul( ) 198 nargs( ) 198 offsetof( ) offsetofbit( ) 199 output_x( ) .200 output_bit( ) 201 output_drive( ) .202 output_float( ) 202 output_high( ) 203 output_low( ) 204 output_toggle( ) 204 perror( ) 205 port_x_pullups ( ) 205 pow( ) pwr( ) 206 printf( ) fprintf( ) 206 psp_output_full( ) psp_input_full( ) psp_overflow( ) 208 putc( ) putchar( ) fputc( ) 209 puts( ) fputs( ) 209 qsort( ) 210 rand( ) 210 read_adc( ) 211 read_bank( ) 212 read_calibration( ) .213 read_configuration_memory( ) 213 read_eeprom( ) 214 ix C Compiler Reference Manual June 2010 read_program_eeprom( ) .214 read_program_memory( ) 215 read_external_memory( ) .215 realloc( ) .215 reset_cpu( ) 216 restart_cause( ) .217 restart_wdt( ) 217 rotate_left( ) 218 rotate_right( ) 219 rtc_alarm_read 219 rtc_alarm_write( ) 220 rtc_read( ) 220 rtc_write( ) .221 rtos_await( ) 221 rtos_disable( ) .222 rtos_enable( ) 222 rtos_msg_poll( ) 223 rtos_msg_read( ) 223 rtos_msg_send( ) 224 rtos_overrun( ) 224 rtos_run( ) 225 rtos_signal( ) 225 rtos_stats( ) 226 rtos_terminate( ) 226 rtos_wait( ) 227 rtos_yield( ) 227 set_adc_channel( ) .228 set_power_pwmx_duty( ) .228 set_power_pwm_override( ) 229 set_pwm1_duty( ) set_pwm2_duty( ) set_pwm3_duty( ) set_pwm4_duty( ) set_pwm5_duty( ) 229 set_rtcc( ) set_timer0( ) set_timer1( ) set_timer2( ) set_timer3( ) set_timer4( ) set_timer5( ) 230 set_timerx( ) 231 set_tris_x( ) 232 set_uart_speed( ) 233 setjmp( ) .233 setup_adc(mode) 234 setup_adc_ports( ) 234 setup_ccp1( ) setup_ccp2( ) setup_ccp3( ) setup_ccp4( ) 236 setup_ccp5( ) setup_ccp6( ) 236 setup_comparator( ) .238 setup_counters( ) 239 setup_dac( ) 240 setup_external_memory( ) 240 setup_lcd( ) 241 setup_low_volt_detect( ) 242 setup_oscillator( ) 242 setup_opamp1( ) setup_opamp2( ) .243 x EXAMPLE PROGRAMS ///////////////////////////////////////////////////////////////// /// EX_STWT.C /// /// This program uses the RTCC (timer0) and interrupts /// /// to keep a real time seconds counter A simple stop /// /// watch function is then implemented Configure the /// /// CCS prototype card as follows, insert jumpers from: /// /// 11 to 17 and 12 to 18 /// ///////////////////////////////////////////////////////////////// #include #use delay (clock=20000000) #use rs232(baud=9600, xmit=PIN_A3, rcv=PIN_A2_ #define INTS_PER_SECOND 76 //(20000000/(4*256*256)) byte seconds; //Number of interrupts left //before a second has elapsed #int_rtcc clock_isr() { //This function is called //every time the RTCC (timer0) //overflows (255->0) //For this program this is apx //76 times per second if( int_count==0) { ++seconds; int_count=INTS_PER_SECOND; } } main() { byte start; int_count=INTS_PER_SECOND; set_rtcc(0); setup_counters (RTCC_INTERNAL, RTCC_DIV_256); enable_interrupts (INT_RTCC); enable_interrupts(GLOBAL) { printf ("Press any key to begin \n\r"); getc(); start=seconds; printf("Press any key to stop \n\r"); getc(); printf ("%u seconds \n\r", seconds-start); } while (TRUE); } 337 C Compiler Reference Manual June 2010 ///////////////////////////////////////////////////////////////// /// EX_INTEE.C /// /// This program will read and write to the ’83 or ’84 /// /// internal EEPROM Configure the CCS prototype card as /// /// follows: insert jumpers from 11 to 17 and 12 to 18 /// ///////////////////////////////////////////////////////////////// #include #use delay(clock-100000000) #use rs232 (baud=9600, xmit=PIN_A3, rv+PIN_A2) #include main () { byte i,j,address, value; { printf("\r\n\nEEPROM: \r\n") //Displays contents for(i=0; i