|
|
View previous topic :: View next topic |
Author |
Message |
beaker Guest
|
multi analog channel on 16c771 |
Posted: Mon Dec 04, 2006 2:32 pm |
|
|
using CCS compiler V2.707
Here is my short code that I am having trouble with, I need to sample two analog channels at 12 bit on the 16c771. For some reason, I cannot get the channel switching to work, a single channel of A0 works fine.
comments?
[/code]
*********************************************************************************************
*/
#include <16C771.h>
#use delay (Clock=4000000,restart_wdt)
#fuses HS,WDT,NOPROTECT
//#use fast_io(B)
SET_TRIS_B(0x81);
/*
*********************************************************************************************
* DEFINES
*********************************************************************************************
*/
#DEFINE pot_input PIN_A0 //sets analog input as pin A0
#DEFINE d_out PIN_B7 //sets digital output as pin B7
#DEFINE tx_enable PIN_B4 // was B6
#DEFINE tx_cts PIN_B2
#DEFINE grn_led_toggle PIN_B3
#DEFINE red_led_toggle PIN_B5
#DEFINE batt_input PIN_B0
#byte ADCON0=0x1F
#byte ADCON1=0x9F
#byte ADRESL=0x9E
#byte ADRESH=0x1E
#bit ADFM_BIT = ADCON1.7
#bit GO_BIT= ADCON0.2
#define BytePtr(var,offset) (char *)(&var + offset)
#USE RS232(BAUD=4800, XMIT=PIN_B7, RCV=PIN_B6, STREAM=USB) // serial connect
/*
*********************************************************************************************
* FUNCTIONS
*********************************************************************************************
*/
void init_hardware()
{
//Setup_adc_ports( 0x00 );
//setup_port_a( 0x00);
setup_adc(ADC_CLOCK_INTERNAL);
output_low(tx_enable); // turn off transmitter
output_high(grn_led_toggle);
output_low(PIN_B5);
output_low(PIN_B4);
delay_ms(1000);
output_low(grn_led_toggle);
output_high(red_led_toggle);
delay_ms(1000);
output_low(red_led_toggle);
}
long read_adc_12(void)
{
long value;
ADFM_BIT=1; //right justify the A/D result
GO_BIT=1; //start the a/d conversion
while(GO_BIT); //wait until conversion is done
restart_wdt();
*Byteptr(value,0)=ADRESL; //get lsb of result
*Byteptr(value,1)=ADRESH; //get msb of result
return(value);
}
main()
{
long int value=0,x=5000;
byte ch;
int i,msb,cksum;
long int value2,batt_cntr=0;
short toggle_var=0;
long led_ticker=0;
short low_batt_flag = 0;
init_hardware();
setup_counters(RTCC_INTERNAL,WDT_36MS); // 36 ms WDT
while(1)
{
restart_wdt();
sleep(); // sleep processor for a while.
restart_wdt();
led_ticker = led_ticker + 1;
if((led_ticker > 50)&&(led_ticker <150>= 500) {
batt_cntr = 0;
//set_adc_channel(batt_input);
ADCON0 = 0x11; // 00010001
delay_ms(1);
value = read_adc_12();
if(value <1400>>8); //sends out MSB of value
cksum=ch;
fprintf(USB,"%c",ch);
//delay_ms(1);
restart_wdt();
while(!input(tx_cts)) {
delay_us(1); // loop here until ok to send data and TX is awake and ready
restart_wdt();
}
ch=(int)(value); //sends out LSB of value
cksum=cksum+ch;
fprintf(USB,"%c",ch);
//delay_ms(1);
restart_wdt();
while(!input(tx_cts)) {
delay_us(1); // loop here until ok to send data and TX is awake and ready
restart_wdt();
}
fprintf(USB,"%c",cksum); //sends out checksum value
//delay_ms(1);
restart_wdt();
while(!input(tx_cts)) {
delay_us(1); // loop here until ok to send data and TX is awake and ready
restart_wdt();
}
fprintf(USB,"#"); //sends out ending framing character
//delay_ms(1);
restart_wdt();
batt_cntr = batt_cntr + 1;
while(!input(tx_cts)) {
delay_us(1); // loop here until ok to send data and TX is awake and ready
restart_wdt();
}
toggle_var = !toggle_var;
if(toggle_var == 1) {
// output_high(led_toggle);
}
else {
// output_low(led_toggle);
}
output_low(tx_enable); // turn off transmitter
}
} |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Dec 04, 2006 3:13 pm |
|
|
I don't have vs. 2.707. The closest I have is vs. 2.705. I made the
test program shown below and looked at the .LST file. The A/D
functions appear to produce the correct ASM code. This test program
is only for 8-bit A/D output, but it does show how to switch channels.
Try doing a simple program like this one, to see if you can get it
working with two A/D channels.
Code: |
#include <16C771.h>
#fuses XT,NOWDT,PUT,BROWNOUT
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_B1, rcv=PIN_B0)
#bit ADFM_BIT = 0x9F.7
void main()
{
int result_0;
int result_1;
setup_adc_ports(sAN0 | sAN1 | VSS_VDD);
setup_adc(ADC_CLOCK_DIV_64);
ADFM_BIT = 1; // Right justify the result
while(TRUE)
{
set_adc_channel(0);
delay_us(20);
result_0 = read_adc();
set_adc_channel(1);
delay_us(20);
result_1 = read_adc();
printf("result 0 = %x\n\r", result_0);
printf("result 1 = %x\n\r", result_1);
}
} |
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|