i'm doin a project in which i'm supposed to interface 8051 microcontroller wid a gsm module 2 send sms... the 8051 microcontroller is a silabs development kit and the gsm module is from multi tech. I'm now supposed 2 write a programme in which after i debug and run it should send a simple msg. I have written most of the code but i'm facin a major problem can some 1 help me! thanks a lot.
this is the code:
//----------------------------------------------------------------------------- // Includes //-----------------------------------------------------------------------------
#include <C8051F000.h> // SFR declarations //#include <stdio.h>
//----------------------------------------------------------------------------- // 16-bit SFR Definitions for 'F0xx //-----------------------------------------------------------------------------
sfr16 RCAP2 = 0xCA; // Timer2 capture/reload sfr16 TMR2 = 0xCC; // Timer2
// ---------------------------------------------------------- // Global Constants //-----------------------------------------------------------------------------
#define BAUDRATE 115200 // Baud rate of UART in bps
// SYSCLK = System clock frequency in Hz
#define SYSCLK 11059200L // SYSCLK in Hz
sbit LED=P1^6; // LED
//sbit aaa = P0^0; //----------------------------------------------------------------------------- // Function Prototypes //-----------------------------------------------------------------------------
void OSCILLATOR_Init (void); void PORT_Init (void); void UART0_Init (void); void Transmit(char ch); void init(); unsigned char Receive(); void SendChar(unsigned char b); void CheckOK_01(); void CheckOK_02(); void CheckOK_03();
//----------------------------------------------------------------------------- // Global Variables //-----------------------------------------------------------------------------
#define UART_BUFFERSIZE 64 unsigned char UART_Buffer[UART_BUFFERSIZE]; unsigned char UART_Buffer_Size = 0; unsigned char UART_Input_First = 0; unsigned char UART_Output_First = 0; unsigned char TX_Ready =1; static char Byte; unsigned char OK[30]; int count; int ok1,ok2,ok3,k; int k_01,k_02,k_03,k_04; int j; int ttt[10];
unsigned char sendstring_01[3]="AT\r"; unsigned char sendstring_02[10]="AT+CMGF=1\r"; unsigned char sendstring_03[9]="AT+CMGS="; unsigned char sendstring_04[11]="+6594725249"; unsigned char sendstring_05[2]="hi";
//----------------------------------------------------------------------------- // main() Routine //-----------------------------------------------------------------------------
void main (void) {
// aaa=0; // WDTCN = 0xDE; // Disable watchdog timer // WDTCN = 0xAD;
OSCILLATOR_Init (); // Initialize oscillator PORT_Init (); // Initialize crossbar and GPIO
UART0_Init (); // Initialize UART0
TI=0; RI=0; // for(k=0;k<30;k++) // { // OK[k]=0; // } while(1) { for(k_01=0;k_01<3;k_01++) { SendChar(sendstring_01[k_01]); // Send 3 data from respective variable } // aaa=~aaa; } CheckOK_01();
LED=1;
if (ok1==1) { for(k_02=0;k_02<10;k_02++) { SendChar(sendstring_02[k_02]); // // Send 10 data from respective variable } }
CheckOK_02();
if (ok2==1) { for(k_03=0;k_03<8;k_03++) { SendChar(sendstring_03[k_03]); // Send 8 data from respective variable }
SendChar('"');
for(k_04=0;k_04<11;k_04++) { SendChar(sendstring_04[k_04]); // Send 4 data from respective variable
}
SendChar('"'); SendChar('\r');
LED=1; LED=0;
CheckOK_03();
LED=1; if (ok1==0) { LED=1; }
ttt[4]=10;
//----------------------------------------------------------------------------- // Transmit Routine //-----------------------------------------------------------------------------
void Transmit(char ch) // *ch shows the pointer the array {
// int xxx; // xxx=0; while (ch!='\0') // last character of array wont be null { SBUF=ch; // transmit array at SBUF while(TI==0); // stays until characters transmitted TI=0; // Ready to transmit next. ch++;
// ttt[xxx+3]=1; // xxx++; // LED=~LED; } }
//----------------------------------------------------------------------------- // Receive Routine //-----------------------------------------------------------------------------
unsigned char Receive() { while(RI==0); // RI=0; // return SBUF;
//----------------------------------------------------------------------------- // CheckOK //-----------------------------------------------------------------------------
void CheckOK_01() {
for(k=0;k<8;k++) { Receive(); RI=0;
// OK[k]=SBUF; if(k==5) { OK[5]=SBUF; } if(k==6) {
OK[6]=SBUF; }
if(OK[5]=='O' && OK[6]=='K') ok1=1; else ok1=0;
} void CheckOK_02() {
for(k=0;k<15;k++) { Receive(); RI=0; // OK[k]=0; OK[k]=SBUF;
if(k==13) { OK[k-13]=SBUF; } if(k==14) {
OK[k-13]=SBUF; }
} if(OK[0]=='O' && OK[1]=='K') ok2=1; else ok2=0;
void CheckOK_03() {
for(k=0;k<30;k++) { Receive(); RI=0; // OK[k]=0; OK[k]=SBUF;
// if(k==25) } // OK[0]=SBUF; // }
// } // if(OK[25]=='>') // ok3=1; else // ok3=0;
} void SendChar(unsigned char b) {
SBUF=b; while(TI==0); TI=0; }
//----------------------------------------------------------------------------- // Initialization Subroutines //-----------------------------------------------------------------------------
//----------------------------------------------------------------------------- // OSCILLATOR_Init //----------------------------------------------------------------------------- // // Return Value : None // Parameters : None // // This function initializes the system clock to use an external 22.1184MHz // crystal. // //----------------------------------------------------------------------------- void OSCILLATOR_Init (void) { int i; // Software timer
OSCICN |= 0x80; // Enable the missing clock detector
// Initialize external crystal oscillator to use 22.1184 MHz crystal
OSCXCN = 0x67; // Enable external crystal osc.
for (i = 0; i < 256; i++); // Wait at least 1 ms while (!(OSCXCN & 0x80)); // Wait for crystal osc to settle
OSCICN |= 0x08; // Select external clock source OSCICN &= ~0x04; // Disable the internal osc.
//OSCICN = 0x14; // Select external clock source 00010100 //OSCICN &= ~0x04; // Disable the internal osc.
//----------------------------------------------------------------------------- // PORT_Init //----------------------------------------------------------------------------- // // Return Value : None // Parameters : None // // This function configures the crossbar and GPIO ports. // // Pinout: // // P0.0 digital push-pull UART TX // P0.1 digital push-pull UART RX //----------------------------------------------------------------------------- void PORT_Init (void) { XBR0 = 0x00; // Enable UART0 XBR1 = 0x00; XBR2 = 0x40; // Enable crossbar and weak pull-up
PRT0CF |= 0x03 ; // Set TX and RX tp P0.0 and P0.1 to push-pull respectively PRT1CF |= 0x40; }
//----------------------------------------------------------------------------- // UART0_Init Variable baud rate, Timer 2, 8-N-1 //----------------------------------------------------------------------------- // // Return Value : None // Parameters : None
// baud rate source. // //----------------------------------------------------------------------------- void UART0_Init (void) { CKCON = 0x20; // Timer2 uses the system clock T2CON = 0x34; // Timer2 used for TX and RX, enabled RCAP2 = - ((long) (SYSCLK/BAUDRATE/32)); TMR2 = RCAP2; TR2= 1; // Start Timer2
SCON = 0x50; // 8-bit variable baud rate; // 9th bit ignored; RX enabled // clear all flags TX_Ready = 1; // Flag showing that UART can transmit // EA=1; // ES=1; // SERIAL PORT INTERRUPT ENABLE
// TI=1; }
"I have written most of the code but i'm facin a major problem can some 1 help me! thanks a lot."
1) Why don't you describe what your "major" problem is? Or are we expected to figure that out on our own?
2) What attempts have you made to figure out what the problem is?
3) The parts that may possibly be code seems like a copy/paste from other code. But everything you posted is posted unformatted, since you didn't read the information on how to post source code.
4) A large part of the code you seem to have written starts with // and isn't used. If it is intended to do something, then you need to remove the // infront of the lines. If the lines are not intended to do something, then you should remove them, and focus on the lines that means something. And instead add comments describing why you are doing different things, and what assumptions you have made.
which is very clearly stated:
www.danlhenry.com/.../keil_code.png
Why not repost your code, with the commented out lines removed. It'll probably end up being about a tenth of the size.
Your Receive function looks very minimalistic!
"with the commented out lines removed"
Indeed.
But be sure to use only spaces for layout - TABs will almost certainly not be interpreted correctly. (you can set uVision - and any other decent programmer's editor - to insert spaces when you press the TAB button).
Also be sure that your code builds cleanly - without warnings or errors - before you post it.
And give a clear description of what the code is intended to do, what it actually does, and what steps you have taken to debug it.
For debugging tips, see: www.8052.com/.../169331