📄 twimain.c
字号:
/*函数的使用说明:
1.该twi模块包含四种工作模式,主发,主收,从收,从发,其中主发由
twiWriteByte(从机地址,要发送的数据)实现,主收由twiReadByte(从机地址)实现,从机
的发送与接收均通过中断实现
2.在从机模式下,需调用twi_init()来使能并初始化twi总线
3.在主机模式下,调用twiWriteByte(从机地址,要发送的数据)向指定的从机写入数据,
调用twiReadByte(从机地址)从指定的从机读取数据,然后可通过查询错误状态error_state
的值决定下一步如何操作。
*/
#include "twi.h"
//TWI同程序的接口参数
//receive_data,error_state不用设置
char receive_data=0; // 接收到的数据
char error_state=0; // twi错误状态
//以下三个参数根据需求进行设置
char slave_address=0xC0; // 设置从机地址,Bits 7..1:存放从机地址,Bit 0:最低位为广播识别使能位
char bit_race=0x20; // 设置主机模式的比特率,SCL=CPU频率/(16+2*(TWBR)*4TWPS),TWPS在4的指数位置
char send_data=0x02; // 从机发送模式下从机向主机发送的数据
//要发送的数据
char trandata[num] = {0x00,0x01,0x02,0x03};
//*************************** twi错误状态说明*********************************
// 0:twi传送无错误
// 1:在主发模式下,SLA+W已发送,返回NOT ACK
// 2:在主发模式下,DATA已发送,返回NOT ACK
// 3:在主发模式下,SLA+W或者数据的仲裁失败
// 4:在主收模式下,SLA+R或者数据的仲裁失败
// 5:在主收模式下,SLA+R已发送,返回NOT ACK
// 6:在主发模式下,START信号发送不成功
// 7:在主收模式下,START信号发送不成功
// 8:在主收模式下,数据接受完成
// 9:在主收模式下,数据接受不成功
//*****************************************************************************
//*************************** 延时1ms 程序 *************************************
//******************************************************************************
void Delay_1ms(void)
{
unsigned int i;
for(i = 0;i < (unsigned int)11 * 143 - 2;i ++);
}
//*************************** 延时1s 程序 **************************************
//******************************************************************************
void Delay_Nms(unsigned int i)
{
unsigned int j;
for(j = 0;j < i;j ++)
Delay_1ms();
}
//*************************** I/O 初始化****************************************
//******************************************************************************
void port_init(void)
{
PORTB = 0x2f; //置PB0,PB1,PB2,PB3,PB5为1,PB4,PB6,PB7为0
DDRB = 0x2f; //置PB0,PB1,PB2--SS,PB3--MOSI,PB5--SCK为输出,PB4--MISO为输入
PORTC = 0x0f;
DDRC = 0x0f; //PC4--SDA,PC5--SCL为输入,PC0--PC3全为输出以便测试
PORTD = 0x05; //置PWR,TXEN为1, TRXCE为0
DDRD = 0x07; //置PD3,PD4,PD5输入;PD2--PWR,PD0--TXEN,PD1--TRXCE输出
}
//******************************串口程序****************************************
//******************************************************************************
//UART0 initialize
// desired baud rate: 9600
// actual: baud rate:9600 (0.0%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
UCSRB = 0x00; //disable while setting baud rate
UCSRA = 0x00;
UCSRC = BIT(URSEL) | 0x0E;
UBRRL = 0x47; //set baud rate lo
UBRRH = 0x00; //set baud rate hi
UCSRB = 0x98;
}
//********************串口程序函数实现部分**************************************
//******************************************************************************
void USART_Transmit( unsigned char data )
{
while ( !( UCSRA & (1<<UDRE))); // 等待发送缓冲器为空
UDR = data; // 将数据放入缓冲器,发送数据
}
#pragma interrupt_handler uart0_rx_isr:12
void uart0_rx_isr(void)
{
//uart has received a character in UDR
}
//****************************** 初始化外围 ************************************
//******************************************************************************
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
uart0_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//****************************** 主函数 ***************************************
/*该程序工作于从收模式下,在基于中断的该模式下,cpu顺序接收来自主机的字符,并根
据字符的值去置位PC0--PC3为低,PC0--PC3可以外接LED以显示操作的结果
*/
//******************************************************************************
void main(void)
{
int i;
init_devices();
Delay_Nms(2000);
while(1)
{
if(receive_data == 0x00)
{
PORTC &= ~BIT(PC0);
}
else if(receive_data == 0x01)
{
PORTC &= ~BIT(PC1);
}
else
PORTC |= 0X0f;
/*
USART_Transmit( receive_data ) //给下位机发送接收到的数据,
Delay_1ms();
*/
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -