PIC C Command Summary
PIC C Command Summary
This document will provide summary function calls to operate the PIC 16F877a internal
peripherals as described in ECET 431. The format is simple. The internal blocks are
listed on the block diagram and the commands available are displayed along with a
simple example. NOTE: This guide is not meant to be a comprehensive guide to the
commands, but rather a way of organizing the commands. To look at the exact syntax,
one should look at the hardcopy compiler manuals in BELK 370, or at the help files
that are in the CCS compiler. The peripherals include:
1. Digital I/O
2. Timers
3. Capture and Compare (CCP)
a. Capture mode
b. Compare Mode
c. PWM Mode
4. A/D conversion
5. Analog Comparator
6. USART (Universal Sychronous/Asynchronous Receiver/Transmitter)
7. SSP(Synchronous Serial Port)
8. PSP (Parallel Slave Port)
9. Internal Data EEPROM
1. Digital I/O
On the PIC 16F877a, there are 5 digital I/O ports. They may be accessed as an entire
port, or as individual pins on the port. The commands available are:
2. Timers
For mode fields, use 1 from group1 or’d symbol( |) with 1 from group 2
example:
#fuses WDT
main()
{
setup_wdt(WDT_144MS);
while(1)
{
restart_wdt();
// my code goes here
}}
Capture mode:
CCP_CAPTURE_FE
CCP_CAPTURE_RE
CCP_CAPTURE_DIV_4
CCP_CAPTURE_DIV_16
Compare mode:
CCP_COMPARE_SET_ON_MATCH
CCP_COMPARE_CLR_ON_MATCH
CCP_COMPARE_INT
CCP_COMPARE_RESET_TIMER
PWM Mode:
CCP_PWM
Set_pwmX_duty(value) // sets the duty cycle to the PWM”X” to the 10 but number in “value”
long rise,fall,pulse_width;
#int_ccp2
void isr()
{
rise = CCP_1;
fall = CCP_2;
pulse_width = fall - rise; // CCP_1 is the time the pulse went high
} // CCP_2 is the time the pulse went low
// pulse_width/(clock/4) is the time
void main()
{
printf("\n\rHigh time (sampled every second):\n\r");
setup_ccp1(CCP_CAPTURE_RE); // Configure CCP1 to capture rise
setup_ccp2(CCP_CAPTURE_FE); // Configure CCP2 to capture fall
setup_timer_1(T1_INTERNAL); // Start timer 1
while(TRUE) {
delay_ms(1000);
printf("\r%lu us ", pulse_width/5 );
}
}
void main()
{
setup_ccp1(CCP_COMPARE_CLR_ON_MATCH); // Configure CCP1 in COMPARE mode
setup_timer_1(T1_INTERNAL); // Set up timer to instruction clk
while(TRUE)
{
while(input(PIN_B0)) ; // Wait for keypress
void main() {
char selection;
byte value;
printf("\r\nFrequency:\r\n");
printf(" 1) 19.5 khz\r\n");
printf(" 2) 4.9 khz\r\n");
printf(" 3) 1.2 khz\r\n");
do {
selection=getc();
} while((selection<'1')||(selection>'3'));
setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM
switch(selection) {
case '1' : setup_timer_2(T2_DIV_BY_1, 127, 1);
break;
case '2' : setup_timer_2(T2_DIV_BY_4, 127, 1);
break;
case '3' : setup_timer_2(T2_DIV_BY_16, 127, 1);
break;
}
setup_port_a(ALL_ANALOG);
setup_adc(adc_clock_internal);
set_adc_channel( 0 );
printf("%c\r\n",selection);
while( TRUE ) {
value=read_adc();
printf("%2X\r",value);
}
4. A/D conversion
setup_adc_ports(value) //sets up the pins to be analog, digital, or both
valid values:
NO_ANALOGS, ALL_ANALOG
AN0_AN1_AN2_AN4_AN5_AN6_AN7_VSS_VREF
AN0_AN1_AN2_AN3_AN4
AN0_AN1_AN2_AN4_VSS_VREF
AN0_AN1_AN3
AN0_AN1_VSS_VREF
AN0_AN1_AN4_AN5_AN6_AN7_VREF_VREF
AN0_AN1_AN2_AN3_AN4_AN5
AN0_AN1_AN2_AN4_AN5_VSS_VREF
AN0_AN1_AN4_AN5_VREF_VREF
AN0_AN1_AN4_VREF_VREF
AN0_AN1_VREF_VREF
AN0
AN0_VREF_VREF
ANALOG_RA3_REF
A_ANALOG
A_ANALOG_RA3_REF
RA0_RA1_RA3_ANALOG
RA0_RA1_ANALOG_RA3_REF
ANALOG_RA3_RA2_REF
ANALOG_NOT_RE1_RE2
ANALOG_NOT_RE1_RE2_REF_RA3
ANALOG_NOT_RE1_RE2_REF_RA3_RA2
A_ANALOG_RA3_RA2_REF
RA0_RA1_ANALOG_RA3_RA2_REF
RA0_ANALOG
RA0_ANALOG_RA3_RA2_REF
cutoff 128
neutral_zone 25
main()
{
int reading;
setup_adc_ports(RA0_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);
while(TRUE)
{
reading=read_adc();
printf("%d \r\n", reading);
if(reading<(cutoff-neutral_zone/2))
light_one_led(GREEN);
else if (reading > (cutoff+neutral_zone/2))
light_one_led(RED);
else
light_one_led(YELLOW);
}
}
5. Analog Comparator
#INT_COMP
void isr() {
safe_conditions=FALSE;
printf("WARNING!! Voltage level is above 3.6 V. \r\n");
}
main() {
setup_comparator(A1_VR_OUT_ON_A2);
setup_vref(VREF_HIGH|15);
enable_interrupts(INT_COMP);
enable_interrupts(GLOBAL);
while(TRUE)
{
if(safe_conditions)
printf("Voltage level is below 3.6 V. \r\n");
safe_conditions=TRUE;
delay_ms(500);
}
}
6. Voltage Reference
setup_vref(mode | value) // sets up the voltage reference for analog compare and analog out on Pin
A2 if so desired
valid mode values:
FALSE
VREF_LOW
VREF_HIGH
Value may be any int 0-15
SPI Commands:
value = spi_read(data) // returns value read by SPI port. IF data is added, it is clocked out on the
read.
I2C Commands:
NOTE: all i2c must use the compiler directive #use i2c
MASTER
SLAVE
SCL=pin
SDA=pin
ADDRESS==nn
FAST
SLOW
RESTART_WDT
FORCE_HW
Example I2C: (master side only: slave side waits for clock from master)
i2c_start();
i2c_write(0x90);
i2c_write(0xac);
i2c_write(data);
i2c_stop();
}
void init_temp() {
output_high(DAL_SDA);
output_high(DAL_SCL);
i2c_start();
i2c_write(0x90);
i2c_write(0x51);
i2c_stop();
temp_config(0xc);
}
i2c_start();
i2c_write(0x90);
i2c_write(0xaa);
i2c_start();
i2c_write(0x91);
datah=i2c_read();
datal=i2c_read(0);
i2c_stop();
data=(signed long)datah*100;
data=data+(((datal >> 4 )*(long)50)/16);
data=data*9;
data = (data / 5) + 3200;
return(data);
}
result=psp_input_full()
result=psp_output_full()
result=psp_overflow()
example:
int next_in = 0;
int next_out = 0;
short data_lost = TRUE;
#int_psp
void psp_isr() {
if(psp_overflow())
data_lost=TRUE;
if(psp_input_full()) {
write_bank(2, next_in++, input_D());
if(next_in == BUFFER_SIZE)
next_in = 0;
if(next_in == next_out)
output_high(BUSY_LINE);
}
}
void main() {
setup_adc_ports(NO_ANALOGS);
setup_psp(PSP_ENABLED);
enable_interrupts(GLOBAL);
enable_interrupts(INT_PSP);
output_low(BUSY_LINE);
printf("Waiting for print data... \r\n\n");
while(true)
{
if(next_in!=next_out)
{
putc(read_bank(2,next_out));
if(++next_out==BUFFER_SIZE)
next_out=0;
if(data_lost) {
printf("\r\nData Lost!!!\r\n");
data_lost = FALSE;
}
output_low(BUSY_LINE);
}
}
}