初学M8串口通讯,遇到难题请大家帮忙看看(GSM模块AT指令通讯)
//ICC-AVR application builder : 2006-6-18 下午 06:02:39// Target : M8
// Crystal: 3.6864Mhz
// Author:Anwarye
// Title: SMSReceiver Transmit
#include <iom8v.h>
#include <macros.h>
#include "Constant.h"
#include "define.h"
/*-----------------------------------------------------------------------*/
/////////////////////////// 初始化 ///////////////////////////////
/*-----------------------------------------------------------------------*/
void Port_init(void)
{
PORTB = 0xFF;
DDRB= 0x01;
PORTC = 0x7F; //m103 output only
DDRC= 0x00;
PORTD = 0xFF;
DDRD= 0x82;
}
//TIMER0 initialisation - prescale:1
// WGM: Normal
// desired value: 1uSec
// actual value:1.000uSec (0.0%)
void Timer0_init(void)
{
TCCR0 = 0x00; //stop
TCNT0 = 0xFC; //set count
TCCR0 = 0x01; //start timer
}
//TIMER1 initialisation - prescale:8
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 10mSec
// actual value:9.998mSec (0.0%)
void Timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0xEE; //setup
TCNT1L = 0x01;
OCR1AH = 0x11;
OCR1AL = 0xFF;
OCR1BH = 0x11;
OCR1BL = 0xFF;
ICR1H= 0x11;
ICR1L= 0xFF;
TCCR1A = 0x00;
TCCR1B = 0x02; //start Timer
}
//UART0 initialisation
// desired baud rate: 19200
// actual: baud rate:19200 (0.0%)
// char size: 8 bit
// parity: Disabled
void Uart0_init(void)
{
UCSRB = 0x00; //disable while setting baud rate
UCSRA = 0x00;
UCSRC = 0x86;
UBRRL = 0x0B; //set baud rate lo
UBRRH = 0x00; //set baud rate hi
UCSRB = 0xD8;
//UCSRB = (1<<RXEN)|(1<<TXEN);
}
/*-----------------------------------------------------------------------*/
///////////////////////////串口接收中断处理////////////////////////////
/*-----------------------------------------------------------------------*/
#pragma interrupt_handler uart0_rx_isr:12
void uart0_rx_isr(void)
{
unsigned char tmp;
//uart has received a character in UDR
tmp = UCSRA; // 读UART状态寄存器,清除接收完成标志RXC
if(UDR == 0x0A)
{
return; // 收到0x0A直接丢弃
}
if(UDR == 0x0D)
{
_0D_Flag++;
if((_0D_Flag == 2) || (UDR == '>')) // 收到两个0x0D则本帧数据接收完成
{
_0D_Flag = 0;
//RxBuf = UDR;
RxIndex = 0; // 接收缓冲指针归零
RxStatus = 1; // 接收完成标志置位
if((RxBuf == 'R') && (RxBuf == 'I') && (RxBuf == 'N') && (RxBuf == 'G'))// 收到来电信号,来电标志置位
{
CallInFlag = 1; // 置电话呼入标志
}
}
}
else
{
RxBuf = UDR; // 继续接收数据放入接收缓冲
RxStatus = 0; // 串口接收中
}
return; // 结束接收,直接跳出
}
/*-----------------------------------------------------------------------*/
///////////////////////////串口发送中断处理////////////////////////////
/*-----------------------------------------------------------------------*/
#pragma interrupt_handler uart0_tx_isr:14
void uart0_tx_isr(void)
{
//character has been transmitted
if(!TxStatus) // 如果发送状态空闲(TxStatus = 0),则发送
{
while ( !(UCSRA & (1<<UDRE)) ); // 等待UDR空
UDR = TxBuf;
}
if((TxBuf == 0x0D) || (TxBuf == 0x1A))// 本帧数据发送结束
{
TxEmptyFlag = 1;
TxStatus = 1; // 发送完成状态标志置位
TxIndex = 0; // 发送缓冲区指针归零
return;
}
TxIndex++;
}
/*-----------------------------------------------------------------------*/
///////////////////////////串口发送使能////////////////////////////////
/*-----------------------------------------------------------------------*/
void TxEnable(void)
{
UDR = TxBuf; // 发送缓冲头送入串口发送寄存器,激活开始发送
}
/*-----------------------------------------------------------------------*/
/////////////////////////////发送AT命令 /////////////////////////////////
/*-----------------------------------------------------------------------*/
void Put_AT_command(const unsigned char *atc, unsigned char atlen)
{
unsigned char count;
while(!TxEmptyFlag); // 等代上次发送结束,发送缓冲指针为零则可以发送
for(count = 0;count < atlen;count++) // AT命令窜移入发送缓冲
{
TxBuf = atc;
}
TxBuf = 0x0D; // 发送缓冲窜结尾加“回车”
TxBuf = 0x00; // 发送缓冲窜结束符号
TxEmptyFlag = 0;
TxStatus = 0;
TxIndex = 1; // 发送指针偏移1
TxEnable(); // 激活发送
}
/*-----------------------------------------------------------------------*/
/////////////////////////////比较两个串 /////////////////////////////////
/*-----------------------------------------------------------------------*/
unsigned char StringCompare(unsigned char *str1,const unsigned char *str2,unsigned char strlen)
{
while(strlen--)
{
if((*(str1++)) != (*(str2++))) return(FALSE); // 两个串不相等返回假
}
return(TRUE);
}
/*-----------------------------------------------------------------------*/
///////////////////////////检查AT命令返回//////////////////////////////
/*-----------------------------------------------------------------------*/
unsigned char CheckAtcReturn(void)
{
while(RxStatus); // 等待接收完成
if((RxBuf == 'O') && (RxBuf == 'K'))// 如果收到OK完成
{
RxStatus = 0;
return (TRUE);
}
else
{
return (FALSE);
}
}
void InitMC55(void)
{
unsigned char count = 10;
TxIndex = 0;
RxIndex = 0;
SetIGT;
DelayMs(200); // 置低MC55 IGT口 200mS
ClrIGT;
Put_AT_command(At,AtLen); // 发送AT命令校准串口,如果失败尝试10次后结束
while(!(CheckAtcReturn()));
Put_AT_command(Ate0,Ate0Len); // 关闭MC55串口回显
while(!(CheckAtcReturn()));
DelayMs(200); // 如果不加延时,发送将少一个0x0D
Put_AT_command(At,AtLen); //
while(!(CheckAtcReturn()));
Put_AT_command(At,AtLen); //
while(!(CheckAtcReturn()));
}
//call this routine to initialise all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
Port_init();
//Timer0_init();
//Timer1_init();
Uart0_init();
MCUCR = 0x00;
//GICR= 0x00;
//TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialised
InitMC55();
}
//
void main(void)
{
init_devices();
//insert your functional code here...
while(1)
{
SetLED;
DelayMs(500);
ClrLED;
DelayMs(500);
}
}
感觉发送缓冲,和接收缓冲总被覆盖。。。头大了,请大家帮忙看看 //ICC-AVR application builder : 2006-6-18 下午 06:02:39
// Target : M8
// Crystal: 3.6864Mhz
#include <iom8v.h>
#include <macros.h>
//typedef unsigned char uchar
//typedef unsigned int uint
//typedef unsigned long ulog
#define RX_BUF_SIZE32
#define TX_BUF_SIZE32
unsigned char RxBuf; // 串口接收缓冲
volatile unsigned char RxIndex; // 串口接收缓冲指针
//extern static volatile unsigned char RxBufEmpty_flag; // 接收缓冲空标志
unsigned char TxBuf; // 发送缓冲
volatile unsigned char TxIndex; // 发送缓冲指针
//extern static volatile unsigned char TxBufEmpty_flag; // 发送缓冲空标志
volatile unsigned char RxCompFlag;
volatile unsigned char TxCompFlag;
volatile unsigned char TxEmptyFlag = 1;
volatile unsigned char RxStatus;
volatile unsigned char TxStatus;
static unsigned char _0D_Flag;
static unsigned char CallInFlag;
// 函数声明
extern void DelayMs(unsigned int ms); #include <iom8v.h>
#include <macros.h>
#define SetIGT (PORTD |= (1<<PD7)) // MC55模块点火控制脚,高有效
#define ClrIGT (PORTD &= ~(1<<PD7))
#define SetLED (PORTB |= (1<<PB0)) // LED
#define ClrLED (PORTB &= (1<<PB0))
#define DetRING0 (PORTD & (1<<PD2))
#define FALSE 0
#define TRUE 1
//定义常量,在程序区
const unsigned char At[] = {"AT"};
const unsigned char Ata[] = {"ATA"};
const unsigned char Ath[] = {"ATH"};
const unsigned char Ate0[] = {"ATE0"};
const unsigned char Atv0[] = {"ATV0"};
const unsigned char Atx0[] = {"ATX0"};
const unsigned char Atf[] = {"AT&F"};
const unsigned char Atclip[] = {"AT+CLIP=1"};
const unsigned char Atcsq[] = {"AT+CSQ"};
const unsigned char Atssyn[] = {"AT^SSYNC=1"};
const unsigned char Atsmso[] = {"AT^SMSO"};
const unsigned char Atsnfs[] = {"AT^SNFS=4"};
const unsigned char Atsnfi[] = {"AT^SNFI=4,32767"};
const unsigned char Atcnmi[] = {"AT+CNMI=1,1,0,0,1"};
const unsigned char Atsnfo[] = {"AT^SNFO=3,16384,16384,16384,16384,16384,4,0"};
const unsigned char Atcmgf[] = {"AT+CMGF=0"};
const unsigned char Atcmgs[] = {"AT+CMGS="};
const unsigned char Atcmgr[] = {"AT+CMGR="};
const unsigned char Atcmgl[] = {"AT+CMGL=0"};
const unsigned char Atcmgd[] = {"AT+CMGD="};
const unsigned char Atsnfpt[] = {"AT^SNFPT=0"};
const unsigned char Atclck1[] = {"AT+CLCK=\"SC\",1,"};
const unsigned char Atcpin[] = {"AT+CPIN?"};
const unsigned char Atclck2[] = {"AT+CLCK=\"SC\",2"};
const unsigned char Atcpwd[] = {"AT+CPWD=\"SC\","};
const unsigned char Atclck0[] = {"AT+CLCK=\"SC\",0,"};
const unsigned char Atcpinc[] = {"AT+CPIN="};
const unsigned char Atsblk[] = {"AT^SBLK"};
// 定义AT命令串长度
//const unsigned char AtcLength[] = {2,3,3,4,4,4,4,9,6,10,7,9,15,17,43,9,8,8,9,8,10,15,8,14,13,15,8,7};
#define AtLen 2
#define AtaLen 3
#define AthLen 3
#define Ate0Len 4
#define Atv0Len 4
#define Atx0Len 4
#define AtfLen 4
#define AtclipLen 9
#define AtcsqLen 6
#define AtssynLen 10
#define AtsmsoLen 7
#define AtsnfsLen 9
#define AtsnfiLen 15
#define AtcnmiLen 17
#define AtsnfoLen 43
#define AtcmgfLen 9
#define AtcmgsLen 8
#define AtcmgrLen 8
#define AtcmglLen 9
#define AtcmgdLen 8
#define AtsnfptLen 10
#define Atclck1Len 15
#define AtcpinLen 8
#define Atclck2Len 14
#define AtcpwdLen 13
#define Atclck0Len 15
#define AtcpincLen 8
#define AtcsblkLen 7 问题解决了,等搞好了在上传,请版组先删除,免得误人子弟。呵呵
-----此内容被xininye于2006-06-25,23:11:31编辑过 我现在 也在学习 MC55 对于如何发AT指令还不是很熟,在程序的实现上还很模糊,能不能把你的思路说一下? mark {:smile:}mark 楼主搞好了吗?求上传。。。 这贴估计话成骨灰了{:funk:}论坛里很多资料 我也在虚心学习中。。。求上传!
页:
[1]