📄 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=0xa0; // 设置从机地址,Bits 7..1:存放从机地址,Bit 0:最低位为广播识别使能位
char bit_race=0x20; // 设置主机模式的比特率,SCL=CPU频率/(16+2*(TWBR)*4TWPS),TWPS在4的指数位置
char send_data=0x02; // 从机发送模式下从机向主机发送的数据
//*************************** 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)8 * 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)
{
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x80; //定义PC.7为输出口,输出0时二极管点亮
DDRC = 0x80;
PORTD = 0x00;
DDRD = 0x00;
}
//******************************串口程序****************************************
//******************************************************************************
//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
}
//****************************** 主函数 ***************************************
//******************************************************************************
void main(void)
{
init_devices();
Delay_Nms(2000);
while(1)
{
twiWriteByte(0xc0,8);
twiReadByte(0xc1);
if(receive_data==2)
PORTC = PORTC&0x7f; //如果收到成功,熄灭PC7*/
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -