![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
scanan
Joined: 13 Aug 2004 Posts: 58 Location: Turkey
|
Problem with UART2 on PIC18F6310 |
Posted: Sat Dec 26, 2015 9:06 am |
|
|
Hello ,
controller: PIC18F6310
compiler version 4.135
I am using 3 serial ports, 2 hardware, one software just for debugging.
I need of 2 serial communication with interrupts on my application.
The #int_rda is working fine and connected to an RFID reader
then #int_rda2 don't function which is connected to GSM modem.
when
enable_interrupts(INT_RDA2);
the PIC controller hangs on.
and here is the definitions
Code: |
#include <18F6310.h>
#device adc=8
#FUSES WDT64
#FUSES INTRC_IO //High speed osc with HW enabled 4X PLL
#FUSES NOBROWNOUT //Reset when brownout detected
#FUSES PUT //No Power Up Timer
#FUSES NOSTVREN //Stack full/underflow will cause reset
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOIESO //Internal External Switch Over mode enabled
#FUSES NOFCMEN //Fail-safe clock monitor enabled
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES PROTECT //Code not protected from reading
#FUSES NOLPT1OSC //Timer1 configured for low-power operation
#FUSES NOMCLR //Master Clear pin disabled
//#pin_select U2TX=PIN_G1
//#pin_select U2RX=PIN_G2
#use delay(clock=32000000)
//#use rs232(uart1,baud=9600, ERRORS, STREAM=RFID)
//#use rs232(uart2, baud=9600, ERRORS, STREAM=GSM)
#use rs232(uart1,stream=RFID,baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8, ERRORS)
#use rs232(uart2,stream=GSM,baud=9600,parity=N,xmit=PIN_G1,rcv=PIN_G2,bits=8, ERRORS)
#use rs232(stream=DEBUG,baud=9600,parity=N,xmit=PIN_C1,rcv=PIN_C2,bits=8, ERRORS)
//#use i2c(Master,Fast=400000,sda=PIN_F3,scl=PIN_F2,force_sw)
|
this is my main
Code: |
void main(void)
{
unsigned int8 i;
//unsigned char tmpbcd;
init_prog();
//delay_ms (100);
// DS3231_init();
// lcd_init ();
EXT_INT_EDGE(2,L_TO_H);
disable_interrupts(INT_EXT2);
enable_interrupts(INT_TIMER0);
enable_interrupts(INT_RDA);
enable_interrupts(INT_RDA2);
enable_interrupts(GLOBAL);
fprintf(RFID,"SRA\r");
for(i=0;i<16;i++)
{
RFIDmsg[i]=' ';
fprintf(DEBUG,"%d\r\n",i);
}
for(;;)
{
/*
IOpin.buzzer=1;
delay_ms(1000);
IOpin.buzzer=0;
delay_ms(1000);*/
// fprintf(DEBUG,"ALGIFARM\r\n");
IOpin.RFIDpower=0;
if(RFIDmsgready)
{
fprintf(DEBUG,"%s\r\n",RFIDmsg);
RFIDmsgready=0;
}
if(GSMmsgready)
{
fprintf(DEBUG,"%s",GSMmsg);
GSMmsgready=0;
}
fprintf(GSM,"AT\r");
//IOpin.cpuLED=1;
delay_ms(3000);
fprintf(DEBUG,"ALGIFARM\r\n");
//IOpin.cpuLED=1;
//sleep();
}
}
|
any help will be appreciated.
cheers _________________ Dr Suleyman CANAN
R&D Electronic Engineer
https://suleymancanan.wordpress.com
Do whatever you do with amateur spirit -
But always feel professional. |
|
![](templates/subSilver/images/spacer.gif) |
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Sat Dec 26, 2015 10:09 am |
|
|
You did not post your RDA HANDLER code.
How can we tell where your problem is when the code is very incomplete? |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9174 Location: Greensville,Ontario
|
|
Posted: Sat Dec 26, 2015 10:26 am |
|
|
Have to agree, we need to see the program not just bits of it as you've got 3 ISRs enabled but no handlersd( code for them)...
Also are you running the PIC on 5 volts? Potentially the GSM Modem is a 3 volt device so you'll need( MUST HAVE) some form of logic level conversion. Now to me a 'modem' usually is an RS232 compatible device but these days anything goes.
Bottom line, we need a lot more inforamtion to decide why/hw the program is not working as expected.
Jay |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19369
|
|
Posted: Sat Dec 26, 2015 12:42 pm |
|
|
First, you don't need pins and UART numbers. Just the latter.
Then critical thing is that the interrupt must use the stream name.
The #pin select can only be used on chips with relocatable peripherals. Won't work on this chip.
Then you must only enable interrupts for peripherals that have handlers. You don't show us any handlers. Enabling a interrupt without a handler will almost certainly hang the chip.
You have the watchdog enabled, long delays, but nothing to reset it. This will cause the chip to never work....
So:
Code: |
#include <18F6310.h>
#device adc=8
#FUSES WDT64
#FUSES INTRC_IO //High speed osc with HW enabled 4X PLL
#FUSES NOBROWNOUT //Reset when brownout detected
#FUSES PUT //No Power Up Timer
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOIESO //Internal External Switch Over mode enabled
#FUSES NOFCMEN //Fail-safe clock monitor enabled
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES PROTECT //Code not protected from reading
#FUSES NOLPT1OSC //Timer1 configured for low-power operation
#FUSES NOMCLR //Master Clear pin disabled
#use delay(INTERNAL=32000000)
#use rs232(UART1,BAUD=9600, BITS=8, ERRORS, STREAM=RFID)
#use rs232(UART2, BAUD=9600, BITS=8, ERRORS, STREAM=GSM)
#use rs232(stream=DEBUG,baud=9600,parity=N,xmit=PIN_C1,rcv=PIN_C2,bits=8, ERRORS)
#INT_RDA
void rxuart1(void)
{
int rx;
rx = fgetc(RFID); //at the minimum this must happen
//for real you want a buffer handler
}
#INT_RDA2
void rxuart1(void)
{
int rx2;
rx = fgetc(GSM); //at the minimum this must happen
//for real you want a buffer handler
}
void main(void)
{
unsigned int8 i;
init_prog();
enable_interrupts(INT_RDA);
enable_interrupts(INT_RDA2);
enable_interrupts(GLOBAL);
//You [u]must[/u] only enable interrupts for which you have got handlers
//Then your code
}
|
You also show an attempt (removed) to sleep the chip. What is going to wake this?. |
|
![](templates/subSilver/images/spacer.gif) |
scanan
Joined: 13 Aug 2004 Posts: 58 Location: Turkey
|
|
Posted: Mon Dec 28, 2015 2:48 am |
|
|
Sorry for my mindedness. I forgot to send the handlers.
My complete Test code are as below.
the .h file
Code: |
#include <18F6310.h>
#device adc=8
#FUSES WDT64
#FUSES INTRC_IO //High speed osc with HW enabled 4X PLL
#FUSES NOBROWNOUT //Reset when brownout detected
#FUSES PUT //No Power Up Timer
#FUSES NOSTVREN //Stack full/underflow will cause reset
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOIESO //Internal External Switch Over mode enabled
#FUSES NOFCMEN //Fail-safe clock monitor enabled
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES PROTECT //Code not protected from reading
#FUSES NOLPT1OSC //Timer1 configured for low-power operation
#FUSES NOMCLR //Master Clear pin disabled
//#pin_select U2TX=PIN_G1
//#pin_select U2RX=PIN_G2
#use delay(clock=32000000)
//#use rs232(uart1,baud=9600, ERRORS, STREAM=RFID)
//#use rs232(uart2, baud=9600, ERRORS, STREAM=GSM)
#use rs232(uart1,stream=RFID,baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8, ERRORS)
#use rs232(uart2,stream=GSM,baud=9600,parity=N,xmit=PIN_G1,rcv=PIN_G2,bits=8, ERRORS)
#use rs232(stream=DEBUG,baud=9600,parity=N,xmit=PIN_C1,rcv=PIN_C2,bits=8, ERRORS)
//#use i2c(Master,Fast=400000,sda=PIN_F3,scl=PIN_F2,force_sw)
struct IOpins {
BOOLEAN pinA0; //A0 0
BOOLEAN pinA1; //A1 0
BOOLEAN pinA2; //A2 0
BOOLEAN pinA3; //A3 0
BOOLEAN pinA4; //A4 0
BOOLEAN pinA5; //A5 0
BOOLEAN pinA6; //A6 0
BOOLEAN unused1; //A7 0
BOOLEAN moduleDR1; //B0 0
BOOLEAN Intswitch; //B1 0
BOOLEAN pinB2; //B2 1
BOOLEAN pinB3; //B3 0
BOOLEAN pinB4; //B4 0
BOOLEAN pinB5; //B5 0
BOOLEAN pgmCLK; //B6 0
BOOLEAN pgmDATA; //B7 0
BOOLEAN eepromWP; //C0 0
BOOLEAN debugTX; //C1 0
BOOLEAN debugRX; //C2 0
BOOLEAN SCL; //C3 0
BOOLEAN SDA; //C4 0
BOOLEAN comLED; //C5 0
BOOLEAN rfIDTx; //C6 0
BOOLEAN rfIDRx; //C7 1
BOOLEAN moduleCLK; //D0 0
BOOLEAN moduleDATA; //D1 0
BOOLEAN moduleCS; //D2 0
BOOLEAN moduleCE; //D3 0
BOOLEAN modulePWRUP; //D4 0
BOOLEAN moduleCPS; //D5 0
BOOLEAN moduleCTX; //D6 0
BOOLEAN moduleCSD; //D7 0
BOOLEAN modulePOWER; //E0 0
BOOLEAN rfIDPOWER; //E1 0
BOOLEAN gsmPOWER; //E2 0
BOOLEAN buzzer; //E3 0
BOOLEAN pinE4; //E4 0
BOOLEAN pinE5; //E5 0
BOOLEAN pinE6; //E6 0
BOOLEAN pinE7; //E7 0
BOOLEAN pinF0; //F0 0
BOOLEAN pinF1; //F1 0
BOOLEAN pinF2; //F2 0
BOOLEAN pinF3; //F3 1
BOOLEAN pinF4; //F4 1
BOOLEAN pinF5; //F5 0
BOOLEAN pinF6; //F6 0
BOOLEAN pinF7; //F7
BOOLEAN cpuLED; //G0 0
BOOLEAN gsmTX; //G1 0
BOOLEAN gsmRX; //G2 0
BOOLEAN pinG3; //G3 0
BOOLEAN pinG4; //G4 0
//BOOLEAN unused1; //G5
//BOOLEAN unused2; //G6
//BOOLEAN unused3; //G7
} IOpin;
#byte IOpin=0xF80
#define ADDRESSSIZE 3
#define PAYLOADSIZE 16
unsigned char channel;
unsigned char idH,idL,chXORcod;
unsigned char RFIDmsg[17],RFIDmsg_[17];
unsigned char GSMmsg[24];
short RFIDmsgready,GSMmsgready,txmode=1;
unsigned char databit;
#bit tempdatabit=databit.7
unsigned char TXBuffer[PAYLOADSIZE];
unsigned char RXBuffer[PAYLOADSIZE];
unsigned char nrfaddress[ADDRESSSIZE];
unsigned int8 config_setup[14];
char ShiftReg;
#bit ShiftRegLSB=ShiftReg.0
unsigned int16 cpucounter,lcdlightcounter;
unsigned char sec;
short lcdlightenable=0;
|
and the main program
Code: |
#include <ALGFRM-RFID.h>
#zero_ram
#fill_rom 0x00
#use fast_io(A)
#use fast_io(B)
#use fast_io(C)
#use fast_io(D)
#use fast_io(E)
#use fast_io(F)
#use fast_io(G)
//==============================================================================
#int_RDA
void RDA_isr(void)
{
static char data,cc=0;
data=getc();
RFIDmsg[cc]=data;
cc++;
if(data==0x0D)
{
cc=0;
RFIDmsgready=1;
}
}
//---------- GSM -----------------------
#int_RDA2
void RDA_isr2(void)
{
static char GSMdata,cc=0;
GSMdata=getc();
GSMmsg[cc]=GSMdata;
cc++;
if(GSMdata==0x0D)
{
cc=0;
GSMmsgready=1;
}
}
//---------------------------------
#int_TIMER0
void TIMER0_isr(void)
{
cpucounter++;
if(cpucounter<100)
{
IOpin.cpuLED=1;
IOpin.comLED=1;
}
else
{
IOpin.cpuLED=0;
IOpin.comLED=0;
}
if(cpucounter>=1000)
cpucounter=0;
set_timer0(5);
clear_interrupt(int_timer0);
}
//=============================================================================
void init_prog(void)
{
setup_wdt(WDT_OFF);
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_16|RTCC_8_BIT);// TIMER0
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
setup_low_volt_detect(FALSE);
setup_oscillator(OSC_32MHZ);
set_tris_a(0xFF);//7F
set_tris_b(0xFF); //FF
set_tris_c(0x94);//94
set_tris_d(0xFF); //02
set_tris_e(0xF0); //f0
set_tris_f(0xFF);//ff
set_tris_g(0xFC); //04
output_a(0x00);
output_b(0x00);
output_c(0x00);
output_d(0x00);
output_e(0x00);
output_f(0x00);
output_g(0x00);
}
//------------------------------------------------------------------------------
void main(void)
{
unsigned int8 i;
init_prog();
EXT_INT_EDGE(2,L_TO_H);
disable_interrupts(INT_EXT2);
enable_interrupts(INT_TIMER0);
enable_interrupts(INT_RDA);
enable_interrupts(INT_RDA2);
enable_interrupts(GLOBAL);
fprintf(RFID,"SRA\r");
for(i=0;i<16;i++)
{
RFIDmsg[i]=' ';
fprintf(DEBUG,"%d\r\n",i);
}
for(;;)
{
IOpin.RFIDpower=0;
if(RFIDmsgready)
{
fprintf(DEBUG,"%s\r\n",RFIDmsg);
RFIDmsgready=0;
}
if(GSMmsgready)
{
fprintf(DEBUG,"%s",GSMmsg);
GSMmsgready=0;
}
fprintf(GSM,"AT\r");
delay_ms(3000);
fprintf(DEBUG,"ALGIFARM\r\n");
}
}
|
_________________ Dr Suleyman CANAN
R&D Electronic Engineer
https://suleymancanan.wordpress.com
Do whatever you do with amateur spirit -
But always feel professional. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19369
|
|
Posted: Mon Dec 28, 2015 4:36 am |
|
|
Look what I said:
"Then critical thing is that the interrupt must use the stream name."
Your interrupts are both attempting to get the data from the same (default) stream. So the correct UART does not get read, and the code will then loop forever... |
|
![](templates/subSilver/images/spacer.gif) |
scanan
Joined: 13 Aug 2004 Posts: 58 Location: Turkey
|
|
Posted: Mon Dec 28, 2015 5:22 am |
|
|
Tried what you said Ttelmah. and the chip is working correctly now.
and I want to wake up from sleep when a data is received from UART1. how to do this?
best regards _________________ Dr Suleyman CANAN
R&D Electronic Engineer
https://suleymancanan.wordpress.com
Do whatever you do with amateur spirit -
But always feel professional. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19369
|
|
Posted: Mon Dec 28, 2015 6:48 am |
|
|
From full sleep, you don't...
You have three choices:
1) Use idle mode, from the low power oscillator. Reprogram the UART for this clock rate. Data will then be valid on wake up.
2) Use idle mode from the low power oscillator. Don't reprogram the UART. Characters received till the chip is awake, will be invalid. Can be handled nicely if you send a dummy character to wake the chip.
3) Use idle mode, with the main oscillator still running. Costs a little power, but the UART will receive as normal.
Idle mode is selected by sleep(SLEEP_IDLE);
The oscillator is controlled by setup_oscillator, before initiating sleep.
Look at table 4-1 in the data sheet. You switch from PRI_RUN, to (say) SEC_RUN, and then to SEC_IDLE (so clocking the UART from Timer1). Or from PRI_RUN to PRI_IDLE (leaving the UART clocked normally).
Which is best will depend on how much power you need to save, versus whether you can afford to lose the 'wake up' character. |
|
![](templates/subSilver/images/spacer.gif) |
scanan
Joined: 13 Aug 2004 Posts: 58 Location: Turkey
|
|
Posted: Thu Dec 31, 2015 1:49 am |
|
|
Many thanks for your valuable help. _________________ Dr Suleyman CANAN
R&D Electronic Engineer
https://suleymancanan.wordpress.com
Do whatever you do with amateur spirit -
But always feel professional. |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9174 Location: Greensville,Ontario
|
|
Posted: Thu Dec 31, 2015 6:30 am |
|
|
A simple hardware option to 'low low power' mode may be to just use a higher capacity battery? These days you can get a LOT of electrons packed into a small cell for very few pennies.
I know it goes against the mantra of 'ultra low power' BUT there are hidden dangers in going with too small a battery. One is that when the device 'wakes up', current draw can be magnitudes higher than sleep,causing voltage drops that causes ADC to read wrong or huge delays in the clock 'getting up to speed'.
Having been caught by this 3 decades ago ,I chose to use batteries with 5 times the capacity and got a reliable system that always gave accurate data.
There are more....
Just something to consider....
Jay |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19369
|
|
Posted: Thu Dec 31, 2015 8:45 am |
|
|
Another advantage to going slightly 'OTT' on batteries, is when conditions are not just 'office'. Most batteries deliver well below their 'rating', once temperatures drop significantly. A device that is designed to operate off a small cell, may repeatedly give trouble when the temperature drops.
I have one piece of equipment I use that repeatedly shows this. It uses two CR2032 coin cells, and will operate all summer fine. However come the winter, you can pretty much guarantee that when you go to use it, it'll fail. I've taken to always carrying spare batteries in the winter. Makes it's 'nominal' 3 year+ battery life pointless. If you keep it indoors it'll give this, but in the cold, no. However (conversely), have another piece of kit that only draws similar current, but uses 2*AA cells. On this I used good quality primary batteries and it was still going strong on it's original set, after 5 years, when I replaced them as a 'better safe' measure. |
|
![](templates/subSilver/images/spacer.gif) |
newguy
Joined: 24 Jun 2004 Posts: 1903
|
|
Posted: Thu Dec 31, 2015 9:44 am |
|
|
Try working with "pipeliners". Our internal welding machines have 2 x 12V lead acid gel cell batteries, each rated for 70Ah. Normal cycle is to affix new section of pipe, connect "umbilicals" for things like welding leads, gas, compressed air, etc. in addition to 120Vac that powers an on-board high capacity quick charger for the batteries. The new section of pipe is welded, all umbilicals disconnected, and the machine travels down to the end of the pipe under pneumatic power and the process begins anew. It's important to note that the batteries are supposed to be charged each cycle.
It's common for someone in the field to either forget to connect the 120Vac or deliberately not do it because they mistakenly think that it somehow saves time. The batteries will last anywhere from a few joints to almost all day depending on whether the machine is configured to weld the pipe from the inside or not. When the batteries fall below a certain threshold, the machine dies on the spot, the failsafe nature of the brakes causes them to engage, and the unit is then wedged very firmly in the pipe until some unfortunate soul is forced to crawl down the pipe with an extension cord and an air hose.
This leads to continual calls for "better batteries because these ones are POS." The forum rules forbid me from telling you what POS actually means but I'm sure you'll be able to figure it out.
Sort of like buying a new car with a full tank of gas, driving it until the fuel tank is empty, and then towing it back to the dealership to scream at them because this one is somehow faulty for running out of fuel in the first place. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19369
|
|
Posted: Thu Dec 31, 2015 3:42 pm |
|
|
This (of course), is where the fuel warning light in the car, needs to be replaced on the machine with a klaxon and a synthesised message saying that somebody has been a (suitable word here), and not connected the power....
Have a good New Year. |
|
![](templates/subSilver/images/spacer.gif) |
newguy
Joined: 24 Jun 2004 Posts: 1903
|
|
Posted: Tue Jan 05, 2016 3:51 pm |
|
|
Another charming trait of pipeliners is that warning lights get covered with black tape or removed entirely if they're not equipped with a disable.
....Or smashed with a hammer. |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|