Appendix F. CCS C Program Function Reference

This is a summary of the more commonly used functions available in CCS C Version 4 (January 2007). For more details on how to use the listed functions and others not included here, visit www.ccsinfo.com for a current manual download.

The following apply to all the following tables:

1. All functions require a header file, e.g., 16F877A.H.

Table F.1. Port Input and Output (Requires Chip Header File Only, e.g., 16F877A.H)
FunctionDescriptionExampleComment
WRITE BYTEWrite all bits with 8-bit integeroutput_A(255);A replaced by B, C, D, or E
SET BITWrite output bit high using pin labeloutput_high(PIN_A0);A0 replaced by A1, A2, . . ., A7, B0, . . ., B7, etc.
CLEAR BITWrite output bit low using pin labeloutput_low(PIN_A0);A0 replaced by A1, A2,. . ., A7, B0, . . ., B7, etc.
READ BYTERead input as 8-bit integerabyte = input_A();A replaced by B, C, D, or E
READ BITRead input bit using pin labelabit = input(PIN_A0);A0 replaced by A1, A2, . . ., A7, B0, . . ., B7, etc.
READ DIRECTIONCheck port data direction registerddra = get_tris_a();Any parallel port ddr code can be checked
CHECK BITRead input bitabit = input_state(PIN_D0);Gets I/O bit value
BIT TOGGLEToggle output bitoutput_toggle(PIN_D0);Invert the logic level at the specified pin
BIT OUTPUTChange port bit to outputoutput_drive(PIN_D0);Does not change the existing bit value
FLOAT OUTPUTSet output pin to high impedanceoutput_float(PIN_D0);Allows an external source to control the line
SET PULLUPSSwitch input pull-ups on or offport_a_pullups(TRUE);Input floats to high value, port A or B only
SET DIRECTIONInitialize port bits for input or outputset_tris_a(0x0F);Explicitly sets up data direction register

Table F.2. Analog Inputs (Requires #DEVICE ADC=nn)
FunctionDescriptionExampleComment
SETUPInitialize ADCsetup_adc(ADC_CLOCK_INTERNAL);All modes listed in device header file
PINS SETUPInitialize ADC pinssetup_adc_ports(RA0_ANALOG);All modes listed in device header file
CHANNEL SELECTSelect ADC inputset_adc_channel(0);Channels 0–7 selected via multiplexer
READRead analog inputinval = read_adc();8-bit read 0–255, 10-bit read 0–1024 (#device option)

Table F.3. Timers (Requires Chip Header File Only, e.g., 16F877A.H)
FunctionDescriptionExampleComment
TIMERX SETUPSet up the timer modesetup_timer0(RTCC_INTERNAL | RTCC_DIV_8 );Clock source and prescale ratio
TIMERX READRead a timer register (8 or 16 bits)count0 = get_timer0();Timer numbers (0–5) valid as fitted
TIMERX WRITEPreload a timer register (8 or 16 bits)set_timer0(126);Timer numbers (0–5) valid as fitted
TIMER CCP SETUPSelect PWM, Capture, or Compare modesetup_ccp1(ccp_pwm);See CCS manual for CCP options
TIMER PWM DUTYSet PWM duty cycleset_pwm1_duty(512);512=mark count=50%

Table F.4. RS232 Serial Port (Requires #USE RS232, #USE DELAYS (Clock=nnnnnnnn))
FunctionDescriptionExampleComment
SET BAUD RATESet hardware RS232 port baud ratesetup_uart(19200);Applies to hardware serial port only
SEND BYTEWrite a character to the default portputc(65)Writes ASCII data or control code to serial output
SEND SELECTWrite a character to selected ports = fputc(“A”,01);As preceding, but stream identifier given
PRINT SERIALWrite a mixed messageprintf(“Answer:%4.3d”,n);Write fixed strings and formatted variable values
PRINT SELECTWrite string to selected serial portfprintf(01,“Message”);As preceding, but stream identifier given
PRINT STRINGPrint a string and write it to arraysprintf(astr,“Ans=%d”,n);Print and copy output to character array
RECEIVE BYTERead a character to an integern = getc();Waits for ASCII code from serial input
RECEIVE STRINGRead an input string to character arraygets(spoint);Reads characters into an array at address
RECEIVE SELECTRead an input string to character arrayastring = fgets(spoint,01);As preceding, but string and stream identifier given
CHECK SERIALCheck for serial input activitys = kbhit();Checks for serial input data but does not wait
PRINT ERRORWrite programmed error messageassert(a<3);Generates an error message if condition is FALSE

Table F.5. SPI Serial Port (spi Can Be Replaced by spi2)
FunctionDescriptionExampleComment
SPI SETUPInitialize SPI serial portsetup_spi(spi_master);See CCS manual for full list of options
SPI READReceives data byte from SPI portinbyte = spi_read();Waits for 8-bit data to arrive
SPI WRITESends data byte via SPI portspi_write(outbyte);Writes 8-bit data to SPI serial line
SPI TRANSFERSend and receive via SPIinbyte = spi_xfer (outbyte);See CCS manual for variations
SPI RECEIVEDCheck if SPI data receiveddone = spi_data_is_in();Returns 0 for not done, 1 if done

Table F.6. I2C Serial Port (#USE I2C() If Hardware Peripheral Fitted, #DEFINE for Software Interface)
FunctionDescriptionExampleComment
I2C STARTIssue start command in master modei2c_start();Start a data transmission
I2C WRITESend a single bytei2c_write(outbyte);Send a data byte
I2C READRead a received byteinbyte = i2c_read();Read a data byte
I2C STOPIssue a stop command in master modei2c_stop();Stop the data transmission
I2C POLLCheck to see if byte receivedsbit = i2c_poll();Returns 1 if byte waiting

Table F.7. Parallel Slave Port
FunctionDescriptionExampleComment
PSP ENABLEEnable or disable PSPsetup_psp(PSP_ENABLED);PSP_DISABLED to switch offSET.
SET DIRECTIONSet the PSP data directionset_tris_e(0);For input arg.=0xFF, or mixed mode
OUTPUT READYChecks if output byte is ready to gopspo = psp_output_full();Byte ready: pspo = 1 To write the PSP: PSP_DATA = outbyte;
INPUT READYChecks if input byte is ready to readpspi = psp_input_full();Byte ready: pspi = 1 To read the PSP: inbyte = PSP_DATA;
PSP OVERFLOWChecks for data overwrite errorpspv = psp_overflow();Check to prevent loss of data due to external mistiming

Table F.8. LCD Control (Requires Chip Header File Only, e.g., 16F877A.H)
FunctionDescriptionExampleComment
LCD SETUPSet up LCD internal controlsetup_lcd(LCD_MUX12,1);Number of control lines, clock prescale
LCD LOADSend display data block to LCDlcd_load(lcddata,0,16);Pointer, offset, number of bytes
LCD SYMBOLSend segment bitslcd_symbol(lcddata,dig1)Specify segments individually

Table F.9. Register Manipulation
FunctionDescriptionExampleComment
REGISTER BIT SETSet a selected bitbit_set(num,1);Sets bit b in integer num (8, 16, or 32 bits)
REGISTER BIT CLEARClear a selected bitbit_clear(num,2);Clears bit b in integer num (8, 16, or 32 bits)
REGISTER BIT TESTTest a selected bitflag = bit_test(num,4);Tests bit b in integer num (8, 16, or 32 bits)
REGISTER SWAPSwap nibbles in a byte variableswap(abyte);Result not returned by function

Table F.10. Block Rotate
FunctionDescriptionExampleComment
BLOCK ROTATE LEFTRotates bits of structure leftrotate_left(&lobyte,6);Address of low byte and number of bytes
BLOCK ROTATE RIGHTRotates bits of structure rightrotate_right(&lobyte,10);Address of low byte and number of bytes
BLOCK SHIFT LEFTShift bit left into low bit of structureshift_left(&lobyte,4,1);Address of low byte, number of bytes, bit in
BLOCK SHIFT RIGHTShift bit right into high bit of structureshift_left(&lobyte,4,1);Address of low byte, number of bytes, bit in

Table F.11. Math Functions (#INCLUDE MATH.H)
FunctionDescriptionExampleComment
ABSOLUTE VALUEAbsolute value of integerabres = abs(x);Returns unsigned positive value of signed integer
LONG ABSOLUTEAbsolute value of long integerlongres = labs(x);Returns unsigned positive value of 16-bit integer
FLOAT ABSOLUTEAbsolute value of floatflores = fabs(x);Returns unsigned positive value of signed float
FLOAT CEILINGRound a float up to integerroundup = ceil(afloat);Returns integer from float
FLOAT FLOORRound a float down to integerroundown = floor(afloat);Returns integer from float
INTEGER DIVIDEInteger dividedivres = div(numer,denom);Returns a structure of quotient and remainder
LONG DIVIDELong integer dividelonres = ldiv(lnumer,ldenom);Returns a structure of quotient and remainder
EXPONENTIALExponential functionexpres = exp(x);Returns exp where x is a float
LOG BASE 10Logarithm base-10 functionlogres = log10(x);Returns log10(x) where x is a float
LOG BASE ELogarithm base-e functionlnres = log(x);Returns ln(x) where x is a float
DIVISION MODULUSModulus (remainder) of divisionmodres = fmod(numer,denom);Returns remainder of float division
FRACTION MODULUSBreak up float into integer and fractionmodfres = modf(afloat,&whole);Returns fractional part, stores integer
FRACTION EXPANDBreak up float into integer and fractionfexres = frexp(afloat,&whole);Returns fractional part
BINARY EXPANDMultiply a float by integral power of 2lexres = ldexp(afloat,sint);Returns a float, sint is a signed integer
RAISE TO POWERRaise float to a powerpowres == pow(afloat,apower);Returns a float raised to a power
SQUARE ROOTCalculate the square root of a floatsqrres = sqrt(afloat);Returns positive root
RANDOM NUMBERGenerates a pseudorandom numberany1 = rand();Returns a random integer from sequence
RANDOM SEEDStart value for the “random” sequencesrand(seed);seed is a new start point in the sequence

Table F.12. Trigonometric Functions (#INCLUDE MATH.H)
FunctionDescriptionExampleComment
SINSine functionnum1 = sin(a);Returns sine of angle a given in radians
COSCosine functionnum2 = cos(a);Returns cosine of angle a given in radians
TANTangent functionnum3 = tan(a);Returns tangent of angle a given in radians
ASINArc sine functionang1 = asin(n);Returns the angle in radians whose sine is float n
ACOSArc cosine functionang2 = acos(n);Returns the angle in radians whose cosine is float n
ATANArc tangent functionang3 = atan(n);Returns the angle in radians whose tangent is float n
SINHHyperbolic sine functionhyp1 = sinh(x);Returns hyperbolic sine of float x
COSHHyperbolic cosine functionhyp2 = cosh(x);Returns hyperbolic cosine of float x
TANHHyperbolic tangent functionhyp3 = tanh(x);Returns hyperbolic tangent of float x

Table F.13. Make Integers
FunctionDescriptionExampleComment
MAKE BYTEExtract a byte from long integermybyte = make8(num,3);Extracts byte from 16- or 32-bit integer
MAKE WORDMake a 16-bit integermyword = make16(byte1,byte0);Combine separate bytes into one integer
MAKE LONGMake a 32-bit integermylong=make32(byte3,byte2,byte1,byte0);Combine 4 bytes or two 16-bit integers

Table F.14. Type Conversions (#INCLUDE STDLIB.H)
FunctionDescriptionExampleComment
ASCII TO FLOATASCII to float conversionnum0 == atof(decstring);Converts a decimal number as string into float
ASCII TO INTEGERASCII to 8-bit integer conversionnum1 = atoi(intstring1);Converts an integer given as string into an 8-bit integer
ASCII TO LONGASCII to 16-bit integer conversionnum2 = atol(intstring2);Converts an integer given as string into a 6-bit integer
ASCII TO 32 BITASCII to 32-bit integer conversionnum3 = atoi32(intstring3);Converts an integer given as string into a 32-bit integer

Table F.15. Character Test (#INCLUDE CTYPE.H)
FunctionDescriptionExampleComment
ALPHANUMERIC?Test for alphanumeric charactertest = isalnum(acode);Returns 1 if character code is in ranges 0–9, A–Z, a–z
NUMBER DIGIT?Test for numerical digit charactertest = isdigit(acode);Returns 1 if character code is in range 0–9
LOWER CASE?Test for lower case alphanumerictest = islower(acode);Returns 1 if character code is in range a–z
SPACE?Test for space charactertest = isspace(acode);Returns 1 if character code is a space
UPPER CASE?Test for upper case alphanumerictest = isupper(acode);Returns 1 if character code is in ranges A–Z
HEX DIGIT?Test for hexadecimal digittest = isxdigit(acode);Returns 1 if character code is in ranges 0–9, A–F, a–f
CONTROL?Test for control charactertest = iscntrl(acode);Returns 1 if character code is control code (00 – 1F)
GRAPHIC?Test for printable charactertest = isgraph(acode);Returns 1 if character code is graphical (21 – 7E)
PRINTABLE?Test for printable or space charactertest = isprint(acode);Returns 1 if character code is printable (20 – 7E)
PUNCTUATION?Test for punctuation charactertest = ispunct(acode);Returns 1 if character code is a punctuation code

Table F.16. Search and Sort (#INCLUDE STDLIB.H)
FunctionDescriptionExampleComment
BINARY SEARCHSearch for given value in a data arraybsearch(k,a1,n,w,compit)Find value k in array a1 of n elements of width w
QUICK SORTSort an array into ascending orderqsort(a1,n,w,sort1)Sort array a1 of n elements of width w using function sortit

Table F.17. Processor Controls (Requires Chip Header File Only, e.g., 16F877A.H)
FunctionDescriptionExampleComment
GET ENVIRONMENTGets information about the MCUchip = getenv(device);Peripheral hardware, memory, configuration, etc.
GOTO ADDRESSJump to program memory locationgoto_address(0x1FF0);Jump in ROM, use with caution
LABEL ADDRESSCheck address of program labellabloc = label_address(start);Labels should be used only in exceptional cases
RESET CPURestarts the program from 0reset_cpu();No return
RESTART CAUSEReturns cause of last resetmessage =restart_cause();Messages defined in MCU header file
RESTART WATCHDOGClear watchdog timerrestart_wdt();Periodical operation to prevent MCU watchdog reset
SETUP OSCILLATORSelect internal clock modesetup_oscillator();MCUs with internal clock
SLEEPStops program and waits for resetsleep();Wake up on specific events

Table F.18. Interrupts (Requires Chip Header File, e.g., 16F877A.H & #INT_XXXX)
FunctionDescriptionExampleComment
INTERRUPT DISABLEDisables peripheral interruptdisable_interrupts(int_timer0);Interrupt labels defined in device header file
INTERRUPT ENABLEEnables peripheral interruptenable_interrpts(int_timer0);Interrupt labels defined in device header file
INTERRUPT CLEARClears peripheral interruptclear_interrupt(int_timer0);Interrupt labels defined in device header file
INTERRUPT ACTIVEChecks if interrupt flag is setinterrupt_active(int_timer0);Interrupt labels defined in device header file
INTERRUPT EDGESelects interrupt trigger edgeext_int_edge(H_TO_L);Rising (L_TO_H) or falling (H_TO_L) edge
INTERRUPT JUMPJump to address of ISRjump_to_isr(isr_loc);Use to service multiple interrupts

Table F.19. Memory Read and Write
FunctionDescriptionExampleComment
READ RAM BANKRead a RAM location directlyabyte = read_bank(3,0x20);Alternative variable access
WRITE RAM BANKWrite a byte into user RAMwrite_bank(3,0x20,0xFF);Write to bank 3, address 0x20, data 0xFF
READ DATA EEPROMRead an EEPROM locationabyte = read_eeprom(0x00);Get byte at given address
WRITE DATA EEPROMWrite a byte into EEPROMwrite_eeprom(0x1F,0x9A);Write to nonvolatile memory address, data
READ PROGRAM ROMRead code from program ROMread_program_memory(0x100,copy,4);Get block from program address, copy in RAM

Table F.20. Memory Allocation (#INCLUDE STDLIBM.H)
FunctionDescriptionExampleComment
MEMORY BLOCK ALLOCATEReserves a block of memoryap1 = calloc(25,4);Allocated block=25×5 bytes
MEMORY BLOCK DEALLOCATEReleases a memory blockfree(ap1);Previously allocated at address pointer ap1
MEMORY BYTES ALLOCATEReserves a number of bytesap1 = malloc(14);Allocated block=14 bytes
MEMORY BLOCK COPYCopy a given number of bytesmemcpy(ap1,ap2,n);Copy n bytes from ap1 to ap2
MEMORY BLOCK MOVEMove a given number of bytesmemmove(ap1,ap2,n);Move n bytes from ap1 to ap2
MEMORY BLOCK SETInitialize locations with a given valuememset(ap1,val1, numofb);Loads integer val1 into numof locations from ap1

Table F.21. Special Setup (Requires Chip Header File Only, e.g., 16F877A.H)
FunctionDescriptionExampleComment
SETUP WATCHDOG TIMERInitialize watchdog time-outsetup_wdt(wdt_1152ms);Time-out options from 18 ms to 2.304 sec
RESET WATCHDOG TIMERClear watchdog timer within the program looprestart_wdt();Watchdog timer is normally reset before time-out
SETUP COMPARATORSConnection of analog comparatorssetup_comparator(A0_A3_A1_A2);Selected MCUs only
VOLTAGE REFERENCESpecify the comparator ref. voltagesetup_vref(vref_low|10);Options in device header file
SETUP OPAMPEnable built in op-amp where fittedsetup_opamp1(1);Selected MCUs only
SETUP SLEEPSets sleep delay timesleep_ulpwl(time_in_us);Selected MCUs only
LOW VOLTS DETECTTriggers interrupt if supply lowsetup_low_volt_detect(lvd_33);Selected MCUs only

2. The numerous CAN and USB functions are not included since these interfaces are not typically available in 16 series MCUs.

3. Alternative functions for the same operation:

putc() == putchar()

getc() == getch() == getchar()

output_bit(PIN_XX,1) == output_high(PIN_XX)

output_bit(PIN_XX,0) == output_low(PIN_XX)

get_timer0() == get_rtcc();

set_timer0(nnn) == set_rtcc(nnn);

pow() == pwr()

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.134.77.195