📄 复件 slave.c
字号:
/*
slave:
main.c
芯艺 2004-09-02
*/
#include <avr/io.h>
#include <avr/delay.h>
#include <avr/twi.h>
#include <stdio.h>
#define uint unsigned int
#define uchar unsigned char
#define SET_RED_LED cbi(PORTC,2)
#define CLR_RED_LED sbi(PORTC,2)
#define SET_LFT_LED cbi(PORTC,1)
#define CLR_LFT_LED sbi(PORTC,1)
#define SET_RIT_LED cbi(PORTC,3)
#define CLR_RIT_LED sbi(PORTC,3)
#define FREQ 4
#define TWI_ADDRESS 0X32
FILE *g_hFile;
uchar g_aDisplayBuf[100];
void DelayMs(uint ms)
{
uint i;
for(i=0;i<ms;i++)
_delay_loop_2(FREQ *250);
}
//标准I/O输出函数
int usart_putchar(char c)
{
if(c=='\n')
usart_putchar('\r');
loop_until_bit_is_set(UCSRA,UDRE);
UDR=c;
return 0;
}
//标准I/O输入函数
int usart_getchar(void)
{
loop_until_bit_is_set(UCSRA,RXC);
return UDR;
}
//初始化
void IoInit(void)
{
//端口初始化
DDRC=0X0E;
PORTC=0X3E;
//串行口初始化
UCSRB=_BV(RXEN)|_BV(TXEN);/*(1<<RXCIE)|(1<<TXCIE)|*/
UBRRL=25; //9600 baud 6MHz:38 4MHz:25
//UART用于标准I/O输入输出
g_hFile=fdevopen(usart_putchar,usart_getchar,0);
//TWI接口初始化,从器件模式
TWAR=TWI_ADDRESS | _BV(TWGCE);
TWCR=_BV(TWEA) | _BV(TWEN);
}
int main(void)
{
uchar i,j=0;
IoInit();
SET_RED_LED;
while(1)
{
while ((TWCR & _BV(TWINT)) == 0);
i=TW_STATUS;
switch(i)
{
case TW_SR_SLA_ACK:
printf("START\nSLA+W\n");
break;
case TW_SR_DATA_ACK:
if(j==0)
printf("Recved:%d",TWDR);
else
printf(" %d",TWDR);
j++;
break;
case TW_SR_STOP:
printf(";\nSTOP\n\n");
j=0;
break;
default:
printf("error:%x",(int)i);
break;
}
TWCR=_BV(TWEA) | _BV(TWEN)|_BV(TWINT);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -