View previous topic :: View next topic |
Author |
Message |
madtoilet
Joined: 02 Apr 2008 Posts: 36
|
help with buffer code |
Posted: Fri Apr 25, 2008 3:32 pm |
|
|
hi,
i'm now trying to figure out the serial buffer code based off the example from the compiler
Code: |
#include "G:\Senior Project\base2\base2.h"
#include <string.h>
#fuses HS,NOWDT,NOPROTECT, NOLVP
#use delay(clock=4mhz)
//#include <lcd.h>
#define BUFFER_SIZE 32
BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;
#int_rda
void serial_isr() {
int t;
buffer[next_in]= getc();
t=next_in;
next_in=(next_in+1) % BUFFER_SIZE;
if(next_in==next_out)
next_in=t; // Buffer full !!
}
#define bkbhit (next_in!=next_out)
BYTE bgetc() {
BYTE c;
while(!bkbhit) ;
c=buffer[next_out];
next_out=(next_out+1) % BUFFER_SIZE;
return(c);
}
void receive_text(void);
void test_gsm(void);
char number[11]="5 ";
char num[] ="0";
unsigned char temp[70] = " ";
void gsm_reading(void);
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
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);
enable_interrupts(int_rda);
enable_interrupts(global);
setup_low_volt_detect(FALSE);
setup_oscillator(False);
output_low(PIN_B6);
delay_ms(500);
output_high(PIN_B6);
delay_ms(2000);
output_low(PIN_B6);
//output_b(0x00);
delay_ms(15000);
receive_text();
//test_gsm();
}
void gsm_reading(void)
{
#use rs232(baud=4800, bits=8, parity=N, stop=1, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
char c;
int count = 1;
output_low(PIN_B0);
output_high(PIN_B3);
//output_high(PIN_B3);
c = bgetc();
for(count= 1; count++; count <11){
c = bgetc();
output_high(PIN_B0);
temp[count] = c;
count++;
}
output_low(PIN_B0);
delay_ms(300);
temp[count] = 0;
} |
i call the receive_text() function and it sends out a command to the GSM module and i should receive a message back. However, when it starts to get serially through the buffer the program gets stuck.
am i missing something? |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Apr 25, 2008 4:19 pm |
|
|
What's in base2.h ? Post it. |
|
![](templates/subSilver/images/spacer.gif) |
madtoilet
Joined: 02 Apr 2008 Posts: 36
|
|
Posted: Fri Apr 25, 2008 5:09 pm |
|
|
Code: | #include <18F4550.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES XT //Crystal osc <= 4mhz
#FUSES NOPROTECT //Code not protected from reading
#FUSES BROWNOUT_NOSL //Brownout enabled during operation, disabled during SLEEP
#FUSES BROWNOUT //Reset when brownout detected
#FUSES BORV46 //Brownout reset at 4.6V
#FUSES NOPUT //No Power Up Timer
#FUSES NOCPD //No EE protection
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES NODEBUG //No Debug mode for ICD
#FUSES LVP //Low Voltage Programming on B3(PIC16) or B5(PIC18)
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES PBADEN //PORTB pins are configured as analog input channels on RESET
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block not protected from table reads
#FUSES NOCPB //No Boot Block code protection
#FUSES MCLR //Master Clear pin enabled
#FUSES LPT1OSC //Timer1 configured for low-power operation
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES PLL1 //No PLL PreScaler
#use delay(clock=4000000)
#use rs232(baud=4800,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
|
this is what is in base2.h |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Apr 25, 2008 5:26 pm |
|
|
Are you using a low voltage programmer ? Most people don't.
(ICD2, ICD-U40, etc., are High Voltage Programmers). Change the
fuse to NOLVP. Do that in all your programs.
You have two #use rs232() statements that use the same pins. Why ?
Post a list of all the RS-232 connections between the PIC and any
external devices. Post the pins used, and the external device (GSM , PC,
etc.) |
|
![](templates/subSilver/images/spacer.gif) |
madtoilet
Joined: 02 Apr 2008 Posts: 36
|
|
Posted: Fri Apr 25, 2008 5:32 pm |
|
|
I had 2 rs232 statements because in my previous code i had issues with the stream=GSM bugging me with my getc(). I know now that i have to use fgetc(GSM); to get get from serial
I'm only using the GSM module connected to pins C6(tx) C7(rx)
Thanks for helping me out ![Smile](images/smiles/icon_smile.gif) |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Apr 25, 2008 5:42 pm |
|
|
You said you call a receive_text() function. I don't see that function
in your posted code. Maybe post a revised program that shows it.
(You don't need to post the base2.h file again). |
|
![](templates/subSilver/images/spacer.gif) |
madtoilet
Joined: 02 Apr 2008 Posts: 36
|
|
Posted: Fri Apr 25, 2008 6:54 pm |
|
|
Code: | void receive_text(void)
{
#use rs232(baud=4800, bits=8, parity=N, stop=1, xmit=PIN_C6, rcv=PIN_C7, stream=GSM, ERRORS)
char c;
int count=1;
int count2=10;
int count3=0;
output_low(PIN_B4);
fprintf(GSM, "AT+CMGF=1");
fprintf(GSM, "\r\n");
delay_ms(2000);
//20 spots to first number
fprintf(GSM, "AT+CMGR=2"); //Read SMS at location 1
fprintf(GSM, "\r\n");
delay_ms(1);
output_high(PIN_B3);
// output_low(PIN_B3);
output_low(PIN_B0);
//output_high(PIN_B3);
//output_high(PIN_B3);
while(!kbhit(GSM)){
//c = fgetc(GSM);
output_high(PIN_B4);
output_low(PIN_B4);
}
while(kbhit(GSM)){
c = fgetc(GSM);
output_high(PIN_B0);
temp[count] = c;
count++;
}
// output_low(PIN_B0);
delay_ms(300);
temp[count] = 0;
fprintf(GSM, "AT+CMGF=1");
fprintf(GSM, "\r\n");
delay_ms(2050);
output_high(PIN_B4);
fprintf(GSM, "AT+CMGS=(my number is here)");
fprintf(GSM, "\r\n");
output_high(PIN_B1);
delay_ms(2000);
fprintf(GSM, "is it empty");
fputc(0x1A,GSM);
output_low(PIN_B3);
}
|
i was re doing my code as shown without the buffer. With the buffer i'd just be replacing fgetc(GSM) with bgetc() i suppose. |
|
![](templates/subSilver/images/spacer.gif) |
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Sat Apr 26, 2008 9:06 am |
|
|
The GSM stuff is just fluff until you get the basic RS232 communications working. Pitch the GSM stuff for now and concentrate on receiving chars into a buffer via an isr. When you understand how to get that to work then move on to adding the GSM code. If you are adding a circular buffer and an isr for the first time it never pays to push luck and expect a complete application to work. Those burned by past failures know to build code incrementally, starting with the essentials and adding complexity only when basic stuff works. Cut your code down to the most basic isr application you can conceive of. Chances are in doing so you will discover your error. |
|
![](templates/subSilver/images/spacer.gif) |
madtoilet
Joined: 02 Apr 2008 Posts: 36
|
|
Posted: Mon Apr 28, 2008 4:28 pm |
|
|
Code: | void test_gsm(void)
{
//#use rs232(baud=4800, bits=8, parity=N, stop=1, xmit=PIN_C6, rcv=PIN_C7, stream=GSM, ERRORS)
//byte c;
char c;
output_low(PIN_B0);
output_low(PIN_B2);
//output_high(PIN_B3);
fprintf(GSM,"AT\r\n");
//delay_ms(10);
output_high(PIN_B2);
c = bgetc();
if(kbhit()){
output_high(PIN_B1);
c = bgetc();
if(kbhit())
output_high(PIN_B0);
}
} |
this is all i'm trying to get my little thingy to do. This is the simplest way to get my pic to receive a character serially. I know that the GSM is responding to my unit with an 'OK' (checked for response with oscilloscope) or if not ok it will give 'ERROR' However, even with KBHIT() i'm not getting a response. I did change my buffer a little
Code: | #include "G:\Senior Project\base2\base2.h"
#include <string.h>
//#fuses HS,NOWDT,NOPROTECT, NOLVP
#use delay(clock=4000000)
// #use rs232(baud=4800, bits=8, parity=N, stop=1, xmit=PIN_C6, rcv=PIN_C7, stream=GSM, ERRORS)
//#include <lcd.h>
#define BUFFER_SIZE 32
BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;
#int_rda
void serial_isr() {
int t;
buffer[next_in]= fgetc(GSM);
t=next_in;
next_in=(next_in+1) % BUFFER_SIZE;
if(next_in==next_out)
next_in=t; // Buffer full !!
}
#define bkbhit (next_in!=next_out)
BYTE bgetc() {
BYTE c;
while(!bkbhit) ;
c=buffer[next_out];
next_out=(next_out+1) % BUFFER_SIZE;
return(c);
}
void receive_text(void);
void test_gsm(void);
char number[11]="5 ";
byte num[] ="5104682725";
char temp[70] = " ";
void gsm_reading(void);
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
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);
enable_interrupts(int_rda);
enable_interrupts(global);
setup_low_volt_detect(FALSE);
setup_oscillator(False);
output_low(PIN_B1);
output_low(PIN_B6);
delay_ms(500);
output_high(PIN_B6);
delay_ms(2000);
output_low(PIN_B6);
output_low(PIN_B3);
delay_ms(150);
//receive_text();
test_gsm();
} |
i made it so it would do c = fgetc(GSM) to get from the stream. Unless i'm mistaken and am not supposed to do this?
my header has rs232 stream = GSM |
|
![](templates/subSilver/images/spacer.gif) |
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Mon Apr 28, 2008 6:17 pm |
|
|
This is not a complete program. It may ( depending on what the undisclosed code does) be missing a way to stop the code from reaching the sleep instruction the compiler places automatically at the bottom of the code. It might be more constructive for the coder to work with simple proven examples of PIC code ( try some of the CCS examples ) then to move on to an actual application. Start with a simple flashing LED program. Acquire the insight that the delay routines will interfere with interrupt timing unless measures are taken. Look at the CCS buffer example and get it working. Then combine the CCS buffer example with the blinking LEDs then and only then with the confidence of having gotten something to work embark on the GSM application needs. |
|
![](templates/subSilver/images/spacer.gif) |
madtoilet
Joined: 02 Apr 2008 Posts: 36
|
|
Posted: Tue Apr 29, 2008 1:35 pm |
|
|
Hey, thanks a bunch for helping me out. I did break the whole thing down and did it slow and i figured out some of it and am receiving the characters like i should. I've still got to figure out some other stuff so i can get this gsm working. Thanks again ![Smile](images/smiles/icon_smile.gif) |
|
![](templates/subSilver/images/spacer.gif) |
|