Jules_Theone
Member
I'm trying to simplify a program to send a number then a letter to a PC to test the labview program I've written but the PIC isn't doing what I expect. This is probably something simple as I'm kinda new to writing in C.
Instead of having to write the for loop between each putch I'm trying to call the delay, but the program just runs at top speed and the serial port cannot keep up. The only delay is from the for loop I've left there.
Code:
/*
*/
#include <htc.h>
__CONFIG(FCMDIS & IESODIS & BORDIS & UNPROTECT & UNPROTECT & MCLRDIS & PWRTDIS & WDTDIS & INTIO);
//Define Variables
int i,j,k,m;
int dlay = 100;
int ADCValue = 0;
int ADCState = 0;
const char Channel[4] = {0x01, 0x01, 0x01, 0x01}; // AN0 only, repeated on all channels (test mode)
//const char Channel[4] = {0x01, 0x05, 0x09, 0x11}; // Channel select: AN0, AN1, AN2, AN4
//const char Ident[4] = {0x31, 0x32, 0x33, 0x34}; // Identifiers are ASCII '1', '2', '3', '4' to test
const char Ident[4] = {0x01, 0x02,0x03,0x04}; // Set Identifiers
void init_comms(void) {
CMCON0 = 0x07; //Comparators off. Digital I/O
PORTA = 0x00; //Clear PORTA
PORTC = 0x30; //Clear PORTC
TRISA = 0x1F; //Set RA4 and RA5 as outputs
TRISC = 0x20; //Set RC5 as an Input, RC4 to an Output
//Desired Baud Rate = 9600, Fosc = 4MHz
//Fosc/[16(n+1)] = Desired Baud Rate; where n = SPBRG
SPBRG = 25; //Set baud rate to 9600
BRG16 = 0; //8-bit Baud Rate Generator is used
BRGH = 1; //High speed
SYNC = 0; //Asynchronous mode
TXEN = 1; //Transmit enabled
SPEN = 1; //Serial port enabled
CREN = 1; //Enables receiver
RX9 = 0; //Selects 8-bit reception
TX9 = 0; //Selects 8-bit transmission
RCIE = 0; //Disables rx interrupts
TXIE = 0; //Disables tx interrupts
PEIE = 0; //Disables interrupts for CPU
//ADC Initialize
ANSEL = 0x00; //Set AN0, AN1, AN2, AN4 as ADC Inputs
/* ADCON0 = 0x01; //Turn on ADC
//Bit 7 - Left Justified
//Bit 6 - Use Vdd as ref.
//Bit 4:2 - Channel 0
//Bit 1 - Do Not Start
//Bit 0 - Turn on ADC
*/ ADCON1 = 0x00; //Select the clock Fosc/8
}
void putch(unsigned char byte)
{
//output 1 byte
while(!TXIF) //set when register is empty
continue;
TXREG = byte;
}
void delay(void)
{
for (i = 0; i < dlay; i++)
{
for (j = 0; j < 255; j++) {};
}
}
void main(void)
{
k = 0;
INTCON = 0;
init_comms();
PORTA = 0x20;
putch(0x00);
putch(0x01);
delay;
putch('A');
{
for (i = 0; i < dlay; i++)
{
for (j = 0; j < 255; j++) {};
}
}
putch(0x02);
delay;
putch('B');
delay;
putch(0x03);
delay;
putch('C');
delay;
putch(0x04);
delay;
putch('D');
PORTA = 0x00;
} //END
Instead of having to write the for loop between each putch I'm trying to call the delay, but the program just runs at top speed and the serial port cannot keep up. The only delay is from the for loop I've left there.
Last edited: