Professional Documents
Culture Documents
Overview
Basic PIC Circuits Development in C using CCS Typical Tasks (A/D, PWM, timing) How to Layout a Program Debugging Tips The Pumpkin protoboard
(resources at end)
Programming in C
Programming the PIC in C offers several
advantages:
Higher level language developer is insulated from
(http://www.ccsinfo.com/) which dont suck. All examples will use CCS code
about everything
#define <16F877.h> // Define the type of chip youre using. // Makes it easier to switch chips // 20Mhz oscillator
/* Initialization Code goes here */ while (TRUE) { /* Program Code goes here */ } }
Picking a channel:
set_adc_channel(0); // Note: must wait between changing // input channels (~ 10us)
Inputting Data:
unsigned int16 data; data = read_adc(); // Declare a 16-bit integer // Read a 10-bit value from the // selected channel
volts = 5.0 * (analog_value / 1024.0); printf(Button pushed! Analog value is %f volts, PWM to %i \n, volts, analog_value); set_pwm1_duty(analog_value); /* Weve pre-configured PWM channel 1 the set_pwm1_duty cycle function accepts a 10-bit number and adjusts the cycle accordingly */ }
#endif
INFO(Button Pushed);
When writing your program in C tell the compiler not to use the top
code from the PC and download your code to the circuit in seconds
This technique is VITAL to preserving sanity
When writing your program in C tell the compiler not to use the top
code from the PC and download your code to the circuit in seconds
This technique is VITAL to preserving sanity
PIC Mad2/Pumpkin
PIC16F877-based Prototyping Board
PIC16F877 microcontroller with PWR/GND connected, 20Mhz
oscillator
8 digital I/O points 8 LEDs (switchable to DI/O) 8 Analog Input ports (also usable as DI/O) 2 PWM channels RS232 interface
print the value to the serial port as a hex number, followed by a newline/return (\r\n)
Use the value read from the analog input channel as the PWM
IDLE
TIM ER
SE RI A
RE _A AD
READ_ANALOG
Enter: when timer1 overflows (every 100 ms) Exit: when analog data is read and PWM updated
RECV_DATA
RE AD +D
AT A
L NA OG
READ ANALOG
is composed with this master on/off state machine. STOP: user sends a stop command START: user sends a start command
STOP
STOPPED START
STARTED
PWM output
In the state handler:
Read in the analog voltage (remember its a 10-bit
http://www.workingtex.com/htpic/PIC16F87x_and_PIC16F7x_bootloader_v7-40.zip