Hi!
I cant seem to get any sensor data from the analog pins of a PIC16F690 into my mac via a serial-to-usb converter. Is there any problems in my code below, anyone? Not sure if have set up the TXREG to out to the tx pin correctly..... please help! Im going crazy!! fabien
I cant seem to get any sensor data from the analog pins of a PIC16F690 into my mac via a serial-to-usb converter. Is there any problems in my code below, anyone? Not sure if have set up the TXREG to out to the tx pin correctly..... please help! Im going crazy!! fabien
Code:
#include <pic.h> // Include HITECH CC header file
__CONFIG (INTIO & WDTDIS & PWRTDIS & MCLRDIS & BORDIS & UNPROTECT & IESODIS & FCMDIS );
unsigned short delay=50; // Initialize on/off delay value to 50 msec
int advalue = 0; // Create A/D storage value and clear it
// FUNCTION PROTOTYPES
void pause( unsigned short msvalue ); // Establish pause routine function before main function
void msecbase( void ); // Establish millisecond base function before main function
void sendserial_twobyte (unsigned char control_number, unsigned char control_data);
// MAIN FUNCTION
main()
{
TRISA = 0b00010111; // RA0, RA1, RA2, RA4 = ins, others out
TRISB = 0b11111111; // all PortC I/O set as outs
TRISC = 0b00001111; // RC0, RC1, RC2, RC3 ins, others outs
ANSEL = 0b11111111; // Select A/D inputs AN0, AN1, AN2, AN3, AN4, AN5, AN6, AN7
ANSELH =0b00000000;
ADCON0 =0b00000000; // left justified
ADCON1 =0b01010000; // set ADC clock source and freq
ADON = 1; // Turn on the AD unit
CM1CON0 = 0; // Comparator 1 off
CM2CON0 = 0; // Coparator 2 off
// ANALOG CHANNEL 1 (AN0)
while(1==1)
{
CHS3 = 0; CHS2 = 0; CHS1 = 0; CHS0 = 1; // set channel AN0
pause(delay); // delay function
GODONE = 1; // start the A-to-D process
while(GODONE) continue;
advalue = ADRESH;// A-to-D result to "advalue"
sendserial_twobyte (240, advalue); // send two bytes
}
// ANALOG CHANNEL 2 (AN1)
while(1==1)
{
CHS3 = 0; CHS2 = 0; CHS1 = 1; CHS0 = 0;
pause(delay);
GODONE = 1;
while(GODONE) continue;
advalue = ADRESH;
sendserial_twobyte (240, advalue);
}
//etc
} // end main loop
void pause( unsigned short msvalue )
{
unsigned short x; // declare a local variable "x"
for (x=0; x<=msvalue; x++) // Loop through a delay equal to msvalue
{ // in milliseconds.
msecbase(); // Jump to millisec delay routine
} // end of for loop
} // end of pause function
void msecbase(void)
{ // start of msecbase function
OPTION = 0b00000001; // set prescaler to TMRO 1:4
TMR0 = 0xd; // preset TMRO to overflow on 250 counts
while(!T0IF); // stay until TMRO overflow flag equals 1
T0IF = 0; // clear the TMR0 overflow flag
} // end of msecbase function
//*******************************************************
//stransmits advalue and anlog number through to tx pin to USB serial on mac
//*******************************************************
void sendserial_twobyte (unsigned char control_number, unsigned char control_data) // no semicolon!
{
while(!TRMT) { } // hold if TXREG full (TRMT = 0 when TXREG full, = 1 when empty)
TXREG = control_number; // send controller number
while(!TRMT) { } // hold again while TXREG full
TXREG = control_data; // send controller data
// this is the bit I cant get into the serial-to-USB converter into the mac...
} // end function