Benzino
Joined: 15 Feb 2010 Posts: 24
|
CCS CAN bus demo board ex_can_ccs_a.c [solved] |
Posted: Tue Mar 02, 2010 12:44 pm |
|
|
Hi,
Has anyone come across with the read_adc() issue using ex_can_ccs_a.c file on the CCS CAN bus demo board?
I have my CAN stuff working for both node a and b (I took node c and d out because I tested only these two).
Note: node a is 18F4580 and node b is 16F876
Using the example from CCS, I was be able to read the ADC on node b without any problems. Below is my code
Code: |
#include <16F876A.h>
#device ADC=10
#fuses HS,NOPROTECT,NOLVP,NOWDT
#use delay(clock=2500000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include "can-mcp2510.c"
#define RED_LED PIN_A1
#define YELLOW_LED PIN_A2
#define GREEN_LED PIN_A3
int16 ms;
#int_timer2
void isr_timer2(void) {
ms++; //keep a running timer that increments every milli-second
}
void main() {
int32 rx_id;
int rx_len, rxstat, buffer[8];
int out_data[8];
int16 i;
output_bit(YELLOW_LED, 1);
output_bit(GREEN_LED, 1);
output_bit(RED_LED, 1);
buffer[0] = 0x00;
buffer[1] = 0x00;
buffer[2] = 0x00;
buffer[3] = 0x00;
buffer[4] = 0x00;
buffer[5] = 0x00;
buffer[6] = 0x00;
buffer[7] = 0x00;
setup_timer_2(T2_DIV_BY_4,79,16); //setup up timer2 to interrupt every 1ms if using 20Mhz clock
setup_port_a(RA0_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);
printf("\r\nStart CAN initialization");
can_init();
printf("\r\nCAN initialization complete");
enable_interrupts(INT_TIMER2); //enable timer2 interrupt
enable_interrupts(GLOBAL); //enable all interrupts (else timer2 wont happen)
while(TRUE) {
if ( can_kbhit() ) {
if(can_getd(rx_id, &buffer[0], rx_len, rxstat))
if (rx_id == 0x201) {
if(buffer[0]==0x01) // buffer[0]&2
{
printf("\r\nreceive rx=201 data=1");
output_bit(YELLOW_LED, 1);
output_bit(GREEN_LED, 0);
output_bit(RED_LED, 1);
delay_ms(100);
buffer[0] = 0x01;
buffer[1] = 0x00;
buffer[2] = 0x00;
buffer[3] = 0x00;
buffer[4] = 0x00;
buffer[5] = 0x00;
buffer[6] = 0x00;
buffer[7] = 0x00;
}
if(buffer[0]==0x02) // buffer[0]&4
{
printf("\r\nreceive rx=201 data=2");
output_bit(YELLOW_LED, 0);
output_bit(GREEN_LED, 1);
output_bit(RED_LED, 1);
delay_ms(100);
buffer[0] = 0x02;
buffer[1] = 0x00;
buffer[2] = 0x00;
buffer[3] = 0x00;
buffer[4] = 0x00;
buffer[5] = 0x00;
buffer[6] = 0x00;
buffer[7] = 0x00;
}
if(buffer[0]==0x0A) // buffer[0]&8
{
printf("\r\nreceive rx=201 data=A");
output_bit(YELLOW_LED, 1);
output_bit(GREEN_LED, 1);
output_bit(RED_LED, 0);
delay_ms(100);
buffer[0] = 0x0A;
buffer[1] = 0x00;
buffer[2] = 0x00;
buffer[3] = 0x00;
buffer[4] = 0x00;
buffer[5] = 0x00;
buffer[6] = 0x00;
buffer[7] = 0x00;
}
}
if (rx_id == 0x202){
printf("\r\nreceive rx=202");
output_bit(YELLOW_LED, 0);
output_bit(GREEN_LED, 0);
output_bit(RED_LED, 0);
delay_ms(1000);
output_bit(YELLOW_LED, 1);
output_bit(GREEN_LED, 1);
output_bit(RED_LED, 1);
buffer[0] = 0x1A;
buffer[1] = 0x2B;
buffer[2] = 0x3C;
buffer[3] = 0x4D;
buffer[4] = 0x5E;
buffer[5] = 0x6F;
buffer[6] = 0x00;
buffer[7] = 0x00;
}
}
//every one second, send new data if transmit buffer is empty
if ( can_tbe() && (ms > 100))
{
ms=0;
i=can_putd(0x452, buffer, 8,1,0,0); //put data on transmit buffer
printf("\r\n");
if (i != 0xFF) { //success, a transmit buffer was open
for (i=0;i<8;i++) {
printf("%X ",buffer[i]);
}
i=read_adc();
printf("Sending AD reading: %Lu",i);
i=can_putd(0x453, &i, 2,1,0,0);
} else { //fail, no transmit buffer was open
printf("\r\nFAIL on PUTD\r\n");
}
}
}
}
|
Now when I tried to read in ADC from AN0 base on ex_can_ccs_a.c, it stalls on that read_adc() function.
Here is what I received on my Hyper terminal
Code: |
CCS CAN Example
CAN ID: 268 (dec) 010C (hex)
The current CAN ID: 268
Running...
Sending LED input
Read ADC input
|
Here is my code for the node a
Code: |
#include <18F458.h>
#device ADC=10
#fuses HS,NOPROTECT,NOLVP,NOWDT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
//#define CAN_DO_DEBUG TRUE
#include "can-18xxx8.c"
#define PIN_LED1 PIN_A5
#define PIN_LED2 PIN_B5
#define PIN_LED3 PIN_B4
#define LED1_HIGH output_low(PIN_LED1)
#define LED1_LOW output_high(PIN_LED1)
#define LED2_HIGH output_low(PIN_LED2)
#define LED2_LOW output_high(PIN_LED2)
#define LED3_HIGH output_low(PIN_LED3)
#define LED3_LOW output_high(PIN_LED3)
#define BUTTON PIN_A4
#define BUTTON_PRESSED !input(BUTTON)
#define CAN_BATT_PACK1 0x104 // Tx 260 (dec)
#define CAN_BATT_PACK2 0x106 // Tx 262 (dec)
#define CAN_BATT_PACK3 0x108 // Tx 264 (dec)
#define CAN_BATT_PACK4 0x10A // Tx 266 (dec)
#define CAN_BATT_PACK5 0x10C // Tx 268 (dec)
#define CAN_BATT_PACK6 0x10E // Tx 270 (dec)
#define CAN_BATT_PACK7 0x110 // Tx 272 (dec)
#define CAN_BATT_EMERG 0x96 // Tx 150 (dec)
int16 ms;
#int_timer2
void isr_timer2(void) {
ms++; //keep a running timer that increments every milli-second
}
// Setup CAN ID for the board looking at DIP switch input from port D
long setup_CANID()
{
int i;
i = input_d();
switch (i)
{
case 1: printf("\r\nCAN ID: %Lu (dec) %LX (hex)", CAN_BATT_PACK1, CAN_BATT_PACK1);
return CAN_BATT_PACK1;
case 2: printf("\r\nCAN ID: %Lu (dec) %LX (hex)", CAN_BATT_PACK2, CAN_BATT_PACK2);
return CAN_BATT_PACK2;
case 4: printf("\r\nCAN ID: %Lu (dec) %LX (hex)", CAN_BATT_PACK3, CAN_BATT_PACK3);
return CAN_BATT_PACK3;
case 8: printf("\r\nCAN ID: %Lu (dec) %LX (hex)", CAN_BATT_PACK4, CAN_BATT_PACK4);
return CAN_BATT_PACK4;
case 16: printf("\r\nCAN ID: %Lu (dec) %LX (hex)", CAN_BATT_PACK5, CAN_BATT_PACK5);
return CAN_BATT_PACK5;
case 32: printf("\r\nCAN ID: %Lu (dec) %LX (hex)", CAN_BATT_PACK6, CAN_BATT_PACK6);
return CAN_BATT_PACK6;
case 64: printf("\r\nCAN ID: %Lu (dec) %LX (hex)", CAN_BATT_PACK7, CAN_BATT_PACK7);
return CAN_BATT_PACK7;
case 128: printf("\r\nCAN ID: %u (dec) %LX (hex)", CAN_BATT_EMERG, CAN_BATT_EMERG);
return CAN_BATT_EMERG;
default: printf("\r\nIncorrect DIP switch selection, assigned CAN ID %Lu (dec) %LX (hex) by DEFAULT", CAN_BATT_PACK1, CAN_BATT_PACK1);
return CAN_BATT_PACK1;
}
}
void main() {
struct rx_stat rxstat;
long rx_id;
long tx_id;
int buffer[8];
int rx_len;
int16 i;
for(i=0;i<8;i++) {
buffer[i]=0;
}
LED1_HIGH;
LED2_HIGH;
LED3_HIGH;
printf("\r\n\r\nCCS CAN EXAMPLE\r\n");
delay_ms(1000);
LED1_LOW;
LED2_LOW;
LED3_LOW;
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);
delay_us(20);
setup_timer_2(T2_DIV_BY_4,79,16); //setup up timer2 to interrupt every 1ms if using 20Mhz clock
can_init();
tx_id = setup_CANID();
printf("\r\nThe current CAN ID: %Lu", tx_id);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
printf("\r\nRunning...");
// Test data
buffer[0] = 0x02;
buffer[1] = 0x00;
buffer[2] = 0x00;
buffer[3] = 0x00;
buffer[4] = 0x00;
buffer[5] = 0x00;
buffer[6] = 0x00;
buffer[7] = 0x00;
while(TRUE)
{
if ( can_kbhit() )
{
}
if ( can_tbe() && (ms > 1000)) //every two seconds, send new data if transmit buffer is empty
{
ms=0;
printf("\r\nSending LED input");
can_putd(0x201, buffer,8,1,0,0);
printf("\r\nRead ADC input");
i = read_adc();
delay_us(20);
printf("\r\nSending ADC reading: %Lu", i);
can_putd(tx_id, &i,2,1,0,0);
}
if (BUTTON_PRESSED) {
while (BUTTON_PRESSED) {}
//delay_ms(200);
printf("\r\nButton is pressed");
tx_id = setup_CANID();
printf("\r\nThe current CAN ID: %Lu", tx_id);
}
}
}
|
There is a few changes on can-18xxx8.c which is the CAN ID type i.e. from extended to standard.
All the hardware components are from CCS CAN bus demo board except additional 8-bit DIP switch connected to port D.
I went around and found a few posts related to ADC that I am looking for e.g. "problem with A/D" posted by PCM programmer and tried to adjust my code similar to it but nothing shows up.
Is there a way to debug read_adc() function? or any suggestion of what might cause this issue? I tried fews things before I post it on the forum but none are helpful.
Please advice, |
|