|
|
View previous topic :: View next topic |
Author |
Message |
kcj
Joined: 05 Oct 2011 Posts: 33
|
Uart\xbee\clock\debugger problems |
Posted: Wed Oct 05, 2011 5:27 pm |
|
|
Hi All,
I am quite new to programming, pics and CCS compiler but I am learning fast.
I am trying to set up wireless communication between 2 xbees. The router xbee and coordinator have been set up so the router is sending a 22bit packet to the coordinator, who is sending it to rx pin. I am only interested in the 20 and 21 bits, as this is the actual sensor data. The hardware set up has been verified using an Arduino board and code, also using a xbee USB dongle, a single packet looks like this
7E 00 12 92 00 13 A2 00 40 69 6E EF 9A 0B 01 01 00 00 01 02 52 B6
I have tried writing the following code but i am having problems, the clock on my board should be 20Mhz but i only get MCU at 1Mhz when I run the debugger. I got the fuse data from CCS valid fuses. Also I am trying to use the debugger to test the code as I work through it, so I can see each hex but it does not even put up the first test printf ("test")
I know this is alot to ask, but any help on these problems would be appreciated.
Code: |
#include <18f13k22.h>
#device ICD=TRUE
#INCLUDE <stdlib.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES XT //Crystal osc <= 4mhz for PCM/PCH , 3mhz to 10 mhz for PCD
#FUSES PLLEN
#FUSES PCLKEN
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES NOPUT //No Power Up Timer
#FUSES NOBROWNOUT //No brownout reset
#FUSES BORV19
#FUSES HFOFST
#FUSES MCLR //Master Clear pin enabled
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES BBSIZ1K //1K words Boot Block size
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES NOPROTECT_0
#FUSES NOPROTECT_1
#FUSES NOCPB //No Boot Block code protection
#FUSES NOCPD //No EE protection
#FUSES NOWRT0
#FUSES NOWRT1
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block not protected from table reads
#use delay(clock=20Mhz)
#use rs232(DEBUGGER)
#define Gled PIN_C5
#define Yled PIN_C4
int LDRMSB;
int LDRLSB;
int i;
int8 buffer [21];
int8 read2;
char read;
void main()
{
while(true)
{
output_low(YLED); //Test the first part works
printf ("test"); //print test on debugger monitor screen
if (kbhit()) //gets character from buffer
{
output_low(GLED); //tests if last bit of code worked
read = getc();
read2 = atoi(read); //changes character into integer
if (read2 == 0x7E)
{
output_high(YLED);
for(i=1;i<21;i++) //reads all 21 and displays values in debugger
{
output_low(GLED);
read = getc();
printf("%s\n\r",read);
buffer[i]= atoi(read);
printf("%u\n\r",buffer[i]);
}
LDRMSB=buffer[19];
LDRLSB=buffer[20];
}
}
}
} |
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Wed Oct 05, 2011 8:17 pm |
|
|
First off the data packet is in bytes not bits, makes a huge difference, especially to us older guys that used to send 22 bit data !
I don't use the 'debugger' but I'd try a simple 'blinking LED' to get the fuse right. XT generally means 4MHz or less, HS for faster crystals. You have an XT fuse but later say the clock is 20MHz. That could be OK IF you have the PLL option enabled WITH the CPUDIV fuse(if your PIC has PLL !). These newer PICs can be confusing, so many options ! I'm learning about the 18F4550 ( upgrade from the 877), so reading the datasheet is very important. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Oct 06, 2011 6:44 am |
|
|
Apart from the clock frequency problems there are a few other issues: Code: | read = getc();
read2 = atoi(read); //changes character into integer
if (read2 == 0x7E) | This is not correct. atoi() requires a zero terminated string as input, not a single character. For your understanding, the function getc has a confusing name as it is returning a 'character type', i.e. a byte in the range 0-255 and not really a character a-z. In this case conversion from character to integer is not required as the 'character type' is the same as an int8 type.
Code: | output_high(YLED);
for(i=1;i<21;i++) //reads all 21 and displays values in debugger
{
output_low(GLED); | Problem here is that the YLED will be only high for a few assembly instructions, at 20MHz that will be about 1us and invisible to the human eye. |
|
|
kcj
Joined: 05 Oct 2011 Posts: 33
|
|
Posted: Thu Oct 06, 2011 5:45 pm |
|
|
Thanks for the replies guys,
Temtronic, sorry about the bits and bytes, off course it is important. I have tried alot of different configurations and can only get the MCU to be 1Mhz, the oscillator is an external 20Mhz.
I thought #use delay(clock=20M, crystal)would do it and deleted the other fuses and i tried your suggestion of turning an led on for 1 sec and off for 1sec and it was too fast, i changed this too #use delay(clock=1M, crystal) and the timing seems right even thou the freq says MCU 1.00MHz??
Code: | #include <18f13k22.h>
#device ICD=TRUE
#use delay(clock=1M, crystal))
#define Gled PIN_C5
#define Yled PIN_C4
void main()
{
while (true)
{
output_low(YLED);
delay_ms(1000);
output_high(YLED);
delay_ms(1000);
}
} |
Regarding the monitor window, it turns out this is only usefull with a serial connection to the board and i cant use it. I could use watches and select the variable to see the values. I am not sure off any other options.
Regarding the Xbee,serial 2 communication. I connected the Rx xbee and Tx pic, Rx pic to Tx xbee. and am still trying to get it to read th incoming data. The code i am trying to use in CCS have been tries to translate from Arduino romantic light sensor example. ie
Code: | /*ROMANCE LIGHT SENSOR
by Rob Faludi*/
int LED = 11;
int analogValue = 0;
void setup() {
pinMode(LED,OUTPUT);
Serial.begin(9600);
}
void loop() {
if (Serial.available() >= 21) {
if (Serial.read() == 0x7E) {
for (int i = 0; i<18; i++) {
byte discard = Serial.read();
}
int analogHigh = Serial.read();
int analogLow = Serial.read();
analogValue = analogLow + (analogHigh*256);
}
}
if (analogValue > 0 && analogValue <= 350) {
digitalWrite(LED, LOW);
}
if (analogValue > 350 && analogValue <= 750) {
digitalWrite(LED, HIGH);
}
if (analogValue > 750 && analogValue <= 1023) {
digitalWrite(LED, LOW);
}
}
|
ckielstra, thanks for the info over atoi, it makes life easier. I have tried simplifying my code using your suggestion, so i can read the bytes.
Code: |
#include <18f13k22.h>
#device ICD=TRUE
#use delay(clock=1M, crystal))
#use rs232(baud=9600,xmit=PIN_B7,rcv=PIN_B5,ERRORS)
#INCLUDE <stdlib.h>
#define Gled PIN_C5
#define Yled PIN_C4
int data1;
int data2;
int i;
int8 buffer [22];
int read;
void main()
{
while(true)
{
output_low(YLED);
if (kbhit(true))
{
output_low(GLED);
delay_ms(1000);
output_high(GLED);
delay_ms(1000);
read = getc();
if (read == 0x7E)
{
output_high(YLED);
for(i=0;i<21;i++)
{
output_low(GLED);
read = getc();
}
data1=buffer[19];
data2=buffer[20];
}
else
read = getc();
}
else
output_high(YLED);
delay_ms(500);
}
} |
I can now see the values of the buffer, if i add data1,data2 and read to watches and step over the program just now.
Thanks for the help guys |
|
|
|
|
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
|