//ICC-AVR application builder : 2008-9-6 10:26:28
// Target : M128
// Crystal: 16.000Mhz
#include <iom128v.h>
#include <macros.h>
#include "define.h"
void port_init(void)
{
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0x00;
DDRB = 0xff;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x08;
PORTE = 0x00;
DDRE = 0x06;
PORTF = 0x00;
DDRF = 0xff;
PORTG = 0x00;
DDRG = 0x00;
}
void SendAddrDelay(INT16 uiMaxDelay)
{
INT16 uiDelay = uiMaxDelay;
while (uiDelay--);
}
void SendDataDelay(INT16 uiMaxDelay)
{
INT16 uiDelay = uiMaxDelay;
while (uiDelay--);
}
//UART0 initialize
// desired baud rate: 9600
// actual: baud rate:9615 (0.2%)
// char size: 9 bit
// parity: Disabled
void uart0_init(void)
{
UCSR0B = 0x00; //disable while setting baud rate
//UCSR0A = 0x01;
UCSR0A = 0x00;
UCSR0C = 0x06;
UBRR0L = 0x67; //set baud rate lo
UBRR0H = 0x00; //set baud rate hi
UCSR0B = 0x9C;
}
//==============================================================================
//串口0用于接收从机的数据
#define TX_BUFFER_LEN 100
#define RX_BUFFER_LEN 100
#define SOI 0x7e
#define EOI 0x0d
#define RECV_ADDR 0x01
#define RECV_DATA 0x02
volatile static UINT8 g_ucRecCommandKind = 0x00;
volatile static UINT8 g_ucRecSlaveComplete = 0;
volatile static UINT8 g_ucRecSlaveEnable = 0;
static INT8 send_Slave[TX_BUFFER_LEN] = {0x7e,'h','e','l','l','o','3','w',0x0d};
static INT8 recv_Slave[RX_BUFFER_LEN];
volatile static UINT16 g_uiRecSlaveBegin = 0;
volatile static UINT16 g_uiRecSlaveEnd = 0;
//==============================================================================
//volatile static UINT8 g_ucRecSlaveOk = 0;
volatile static UINT8 g_ucRecSlaveAddr = 0;
#pragma interrupt_handler uart0_rx_isr:19
void uart0_rx_isr(void)
{
//uart has received a character in UDR
UINT8 ucRecData;
ucRecData = UDR0;
//PORTB = ~ucRecData;
//接收缓冲区无新数据,且上位机已经发送从机寻址命令,等待从机回送地址
if ((g_ucRecSlaveComplete == 0)
&& (g_ucRecCommandKind == RECV_ADDR))
{
g_ucRecSlaveAddr = ucRecData; //将接收的数据暂存
//PORTB = ucRecData;
g_ucRecSlaveComplete = 1; //等待判断接收的地址与寻址的地址是否相等\
//CLI();
//while (1);
//UCSR0A |= (1 << RXC0);
//UCSR0B &= 0x4f;
}
//上位机已经向从机发送数据,等待从机回送发送的数据
if (g_ucRecCommandKind == RECV_DATA)
{
if(g_uiRecSlaveEnd == RX_BUFFER_LEN) //接收缓冲区满
{
g_uiRecSlaveEnd = 0;
}
//
//g_ucRecComplete g_ucRecEnable
//00 数据处理完毕,允许接收新的数据
//01 正在接收新的数据
//10 接收完数据,正在处理数据
//
if ((ucRecData == SOI) && (g_ucRecSlaveEnable == 0)
&& (g_ucRecSlaveComplete == 0)) //若接收到起始字符且允许接收标志为0
{
//PORTB = ucRecData;
g_uiRecSlaveEnd = 0; //清接收字符个数计数器
g_ucRecSlaveComplete = 0; //清接收完成标志
g_ucRecSlaveEnable = 1; //置允许接收标志以允许接收其它字符
//memset(recv_buffer,0,sizeof(recv_buffer)); //将接收缓冲区清空
recv_Slave[g_uiRecSlaveEnd++] = ucRecData; //保存接收的第一个字符
//PORTB = ucRecData;
}
else if((ucRecData == EOI) && (g_ucRecSlaveEnable == 1)) //若接收到结束字符且允许接收标志为1
{
//PORTB = ucRecData;
g_ucRecSlaveComplete = 1; //置接收完成标志
recv_Slave[g_uiRecSlaveEnd] = ucRecData; //保存最后一个字符
g_ucRecSlaveEnable = 0; //清允许接收标志以禁止接收其它字符
}
else if (g_ucRecSlaveEnable == 1) //若允许接收其它字符
{
g_ucRecSlaveComplete = 0;
recv_Slave[g_uiRecSlaveEnd++] = ucRecData; //将接收的字符保存到接收缓冲区
//PORTB = ucRecData;
}
}
}
//UART1 initialize
// desired baud rate:9600
// actual baud rate:9615 (0.2%)
// char size: 8 bit
// parity: Disabled
void uart1_init(void)
{
UCSR1B = 0x00; //disable while setting baud rate
UCSR1A = 0x00;
UCSR1C = 0x06;
UBRR1L = 0x67; //set baud rate lo
UBRR1H = 0x00; //set baud rate hi
UCSR1B = 0x98;
}
//==============================================================================
//向上位机发送数据
void Uart1Transmit(unsigned char ucData)
{
while (!(UCSR1A & (1 << UDRE1)))
;
UDR1 = ucData;
}
//==============================================================================
//串口1用于接收上位机的数据
#define TX_BUFFER_LEN 100
#define RX_BUFFER_LEN 100
#define SOI 0x7e
#define EOI 0x0d
volatile static UINT8 g_ucRecComplete = 0;
volatile static UINT8 g_ucRecEnable = 0;
static INT8 send_buffer[TX_BUFFER_LEN] = {0x7e,'h','e','l','l','o','3','w',0x0d};
static INT8 recv_buffer[RX_BUFFER_LEN];
volatile static UINT16 g_uiRecBegin = 0;
volatile static UINT16 g_uiRecEnd = 0;
//==============================================================================
#pragma interrupt_handler uart1_rx_isr:31
void uart1_rx_isr(void)
{
//uart has received a character in UDR
UINT8 ucRecData;
ucRecData = UDR1;
Uart1Transmit(UDR1);
if(g_uiRecEnd == RX_BUFFER_LEN) //接收缓冲区满
{
g_uiRecEnd = 0;
}
//
//g_ucRecComplete g_ucRecEnable
//00 数据处理完毕,允许接收新的数据
//01 正在接收新的数据
//10 接收完数据,正在处理数据
//
if ((ucRecData == SOI) && (g_ucRecEnable == 0)
&& (g_ucRecComplete == 0)) //若接收到起始字符且允许接收标志为0
{
g_uiRecEnd = 0; //清接收字符个数计数器
g_ucRecComplete = 0; //清接收完成标志
g_ucRecEnable = 1; //置允许接收标志以允许接收其它字符
//memset(recv_buffer,0,sizeof(recv_buffer)); //将接收缓冲区清空
recv_buffer[g_uiRecEnd++] = ucRecData; //保存接收的第一个字符
}
else if((ucRecData == EOI) && (g_ucRecEnable == 1)) //若接收到结束字符且允许接收标志为1
{
g_ucRecComplete = 1; //置接收完成标志
recv_buffer[g_uiRecEnd] = ucRecData; //保存最后一个字符
g_ucRecEnable = 0; //清允许接收标志以禁止接收其它字符
}
else if (g_ucRecEnable == 1) //若允许接收其它字符
{
g_ucRecComplete = 0;
recv_buffer[g_uiRecEnd++] = ucRecData; //将接收的字符保存到接收缓冲区
}
}
void Uart0Flush(void)
{
UINT8 dummy;
while (UCSR0A & (1 << RXC0))
{
dummy = UDR0;
}
}
//==============================================================================
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
XDIV = 0x00; //xtal divider
XMCRA = 0x00; //external memory
port_init();
uart0_init();
uart1_init();
Uart0Flush();
MCUCR = 0x00;
EICRA = 0x00; //extended ext ints
EICRB = 0x00; //extended ext ints
EIMSK = 0x00;
TIMSK = 0x00; //timer interrupt sources
ETIMSK = 0x00; //extended timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//==============================================================================
//向从机发送地址帧
void SendAddrFrame(unsigned char ucData)
{
while (!(UCSR0A & (1 << UDRE0)))
;
UCSR0B |= (1 << TXB80);
UDR0 = ucData;
没有合适的资源?快使用搜索试试~ 我知道了~
使用485主从机通信,是本人的一个测试程序,功能为自发自收。
共36个文件
hex:2个
dbg:2个
lk:2个
4星 · 超过85%的资源 需积分: 49 169 下载量 141 浏览量
2008-09-07
20:32:21
上传
评论 8
收藏 80KB RAR 举报
温馨提示
使用485主从机通信,是本人的一个测试程序,功能为自发自收,在终端输入~1232445\n,将回显刚发送的字符.
资源推荐
资源详情
资源评论
收起资源包目录
RS485(AVR).rar (36个子文件)
RS485.DSN 79KB
RS485slave
main.c 6KB
main.lis 34KB
RS485slave.lk 9B
main.s 17KB
RS485SLAVE.SRC 81B
RS485slave.prj 889B
define.h 1KB
main._c 6KB
RS485slave.dbg 3KB
RS485slave.lst 19KB
define._h 1KB
main.o 8KB
RS485slave.hex 2KB
RS485slave.mak 747B
main.i 0B
main.dp2 260B
RS485slave.cof 4KB
RS485slave.mp 2KB
RS485master
McuInit.dp2 266B
McuInit.o 14KB
McuInit.s 29KB
RS485master.lk 12B
RS485master.prj 906B
define.h 2KB
RS485master.dbg 4KB
RS485master.hex 4KB
RS485MASTER.SRC 86B
define._h 2KB
McuInit._c 11KB
RS485master.mp 3KB
RS485master.lst 32KB
McuInit.lis 58KB
RS485master.mak 792B
RS485master.cof 7KB
McuInit.c 11KB
共 36 条
- 1
qioujingchao
- 粉丝: 2
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页