|
|
10.0. PIC Software Development |
/* ------------------------------------------------------- | FILE : mark_2.c | | PROJECT : Low Cost PC Based Oscilloscope | | DESC : Test Serial Communications, waits for an | | incoming char and transmits the char. | | ==================================================== | | DATE : 19/02/2002 | | BY : Colin K McCord | | VERSION : 1.0 | -------------------------------------------------------- */ #include <16F877.h> #device PIC16F877 *=16 ADC=10 #fuses HS,NOWDT,NOPROTECT,NOLVP #use delay(clock = 4000000) #use rs232(baud=9600, xmit = PIN_C6, rcv = PIN_C7, parity = N, bits = 8) main() { /* Note kbhit() can be used to check for incoming char before calling getc() */ while(TRUE) { putc(getc()); // Wait for incoming char and transmit.... } } |
/* -------------------------------------------------------- | FILE : mark_3.c | | PROJECT : Low Cost PC Based Oscilloscope | | DESC : Read CH1 ADC, transmit result through | | : RS232 using the real-time frame format. | | ==================================================== | | DATE : 19/02/2002 | | BY : Colin K McCord | | VERSION : 1.1 | -------------------------------------------------------- */ #include <16F877.h> #device PIC16F877 *=16 ADC=10 // use #device adc = 10 to implement a 10-bit conversion, // otherwise the default is 8-bits. #fuses HS,NOWDT,NOPROTECT,NOLVP #use delay(clock = 4000000) #use rs232(baud=9600, xmit = PIN_C6, rcv = PIN_C7, parity = N, bits = 8) main() { // NOTE: by default in CCS all var's are unsigned long int adcValue; // 16-bit storage for ADC reading char adcHI,adcLO; // 8-bit storage for real-time frames. setup_adc_ports(A_ANALOG); // RA0 - RA4 Analogue, RE0 - RE2 digital setup_adc(ADC_CLOCK_INTERNAL); // Use internal ADC clock. set_adc_channel(0); delay_us(20); // Delay for sampling cap to charge while(TRUE) { adcValue = read_adc(); // Get ADC reading /* Convert 16-bit adcValue to Real-time frame structure, CH1 */ adcHI = (char)((adcValue >> 5)& 0x1f); // 0|0|0|d9|d8|d7|d6|d5 adcLO = (char)((adcValue & 0x1f)|0x80); // 1|0|0|d4|d3|d2|d1|d0 putc(adcHI); // Transmit Byte 1 (d9...d5) putc(adcLO); // Transmit Byte 2 (d4...d0) delay_ms(10); // Preset delay, repeat every 10ms } } |
/* -------------------------------------------------------- | FILE : mark_4.c | | PROJECT : Low Cost PC Based Oscilloscope | | DESC : Test RS232, baud rate set by dip switches. | | ==================================================== | | DATE : 01/03/2002 | | BY : Colin K McCord | | VERSION : 1.0 | -------------------------------------------------------- */ #include <16F877.h> #device PIC16F877 *=16 ADC=10 #fuses HS,NOWDT,NOPROTECT,NOLVP #byte PORTE = 0x09 // PortE lives in File 9 /* Note 20MHz clock must be used for 115,000 bps, the % error is to large at slower speeds */ #use delay(clock = 20000000) // 20MHz clock, change this value if using different clock speed. #use rs232(baud=9600, xmit = PIN_C6, rcv = PIN_C7, parity = N, bits = 8) #use fast_io(E) // Fast access to PortE (don't fiddle with TRISE) /* Forward declaration of functions */ void SetBaudRate(); main() { set_tris_e(0x17); // TRISE = 00010111; RE2,RE1 and RE0 TTL Inputs SetBaudRate(); while(TRUE) { /* Note printf could be used but this function is wasteful and will not be used */ putc('T'); // Transmit T putc('e'); // Transmit e putc('s'); // Transmit s putc('t'); // Transmit t putc('i'); // Transmit i putc('n'); // Transmit n putc('g'); // Transmit g putc('.'); // Transmit . putc('.'); // Transmit . putc('.'); // Transmit . delay_ms(1000); // Preset delay, repeat every second } } void SetBaudRate() { switch(PORTE & 0x07) // Read dip switches and setup baud rate { case 0: set_uart_speed(4800); break; case 1: set_uart_speed(9600); break; case 2: set_uart_speed(14400); break; case 3: set_uart_speed(19200); break; case 4: set_uart_speed(32768); break; case 5: set_uart_speed(38400); break; case 6: set_uart_speed(57600); break; case 7: set_uart_speed(115200); break; } } |
/* --------------------------------------------------------
| FILE : mark_7.c |
| PROJECT : Low Cost PC Based Oscilloscope |
| DESC : Test timer interrupts |
| ==================================================== |
| DATE : 05/03/2002 |
| BY : Colin K McCord |
| VERSION : 1.1 |
-------------------------------------------------------- */
#include <16F877.h>
#device PIC16F877 *=16 ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP
#byte PORTE = 0x09 // PortE lives in File 9
/* Note 20MHz clock must be used for 115,000 bps, the % error is to
large at slower speeds */
#use delay(clock = 20000000)
#use rs232(baud=9600, xmit = PIN_C6, rcv = PIN_C7, parity = N, bits = 8)
#use fast_io(E) // Fast access to PortE (don't fiddle
with TRISE)
#define T0_INTS_PER_SEC 76 // (20,000,000/(4*256*256))
#define T1_INTS_PER_SEC 76 // (20,000,000/(4*1*65536))
byte int_count0; // Number of T0 interrupts left before a
0.5s has elapsed.
byte int_count1; // Number of T1 interrupts left before a
0.25s has elapsed.
byte int_count2; // Number of T2 interrupts left before a
100 msec has elapsed.
#int_rtcc // RTCC (timer0) interrupt, called every
time RTCC overflows (255->0)
Timer0_ISR()
{
if(--int_count0==0)
{
printf("Interrupt_T0..."); // Every 0.5
seconds.
int_count0 = T0_INTS_PER_SEC/2;
}
}
#INT_TIMER1 // timer1 interrupt, called every time timer1
overflows (65536->0)
Timer1_ISR()
{
if(--int_count1==0)
{
printf("Interrupt_T1..."); // Every 0.25
seconds.
int_count1 = T1_INTS_PER_SEC/4;
}
}
#INT_TIMER2 // timer2 interrupt
Timer2_ISR()
{
if(--int_count2==0)
{
printf("Interrupt_T2..."); // Every 0.1
seconds.
int_count2 = 100;
}
}
/* Forward declaration of functions */
void SetBaudRate();
main()
{
set_tris_e(0x17); // TRISE = 00010111; RE2,RE1 and RE0 TTL
Inputs
SetBaudRate();
/** Setup timer0 (RTCC) **/
int_count0 = T0_INTS_PER_SEC/2; // 0.5 seconds
set_rtcc(0);
setup_counters(RTCC_INTERNAL,RTCC_DIV_256);
enable_interrupts(RTCC_ZERO);
/** Setup timer1 **/
int_count1 = T1_INTS_PER_SEC/4; //0.25 second
setup_timer_1(T1_DIV_BY_1 | T1_INTERNAL);
set_timer1(0);
enable_interrupts(INT_TIMER1);
/** Setup timer2 **/
int_count2 = 100; // 0.1 second
setup_timer_2 (T2_DIV_BY_4,125,9); // interrupt every
1ms
set_timer2(0);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
while(TRUE)
{
printf("Main...");
delay_ms(1000); // 1 second delay
}
}
void SetBaudRate()
{
switch(PORTE & 0x07) // Read dip switches and setup baud rate
{
case 0: set_uart_speed(4800); break;
case 1: set_uart_speed(9600); break;
case 2: set_uart_speed(14400); break;
case 3: set_uart_speed(19200); break;
case 4: set_uart_speed(32768); break;
case 5: set_uart_speed(38400); break;
case 6: set_uart_speed(57600); break;
case 7: set_uart_speed(115200); break;
}
}
|
Home About me National Record Of Achievement Hobbies / Interests Guest Book Contact Me Links Snooker Amateur Radio Site Map
© 2002 Designed by Colin K McCord |