标题:
基于AVR的SCCB读写程序
[打印本页]
作者:
hujia
时间:
2015-6-23 14:41
标题:
基于AVR的SCCB读写程序
#include<iom16v.h>
#define uchar unsigned char
#define uint unsigned int
#define set_bit(a,b) a|=(1<<b)
#define clr_bit(a,b) a&=(1<<b)
#define get_bit(a,b) a&(1<<b)
#pragma interrupt_handler TX_end:14
uchar TXEND;
void TX_end()
{
TXEND=0;//发送完毕标志
}
void USART_INT()//初始化串口
{
//UCSRA默认
UCSRB=0x48;//使能发送完毕中断,禁能数据寄存器空中断,使能发送模式,关闭接收,关闭接收中断
UCSRC=0x86;//禁止奇偶效验,数据位8,停止位1,工作在异步模式
UBRRH=0x00;//
UBRRL=51;//波特率9600,8MHZ,
SREG=0x80;//开启全局中断
}
void send_byte(uchar buffer)//向PC发送数据
{
while(0==get_bit(UCSRA,5));//数据寄存器里的数据是不是为空如果是就写数据
UDR=buffer;
while(TXEND);//是否发送完毕?
TXEND=1;//上一帧已经发送准备下一帧的发送
}
/*void ov7670_int()//7670初始化函数
{
}*/
////***********SCCB-----OX7670************************//////////
void delay1ms(uint z)
{
uint j,k;
for(k=z;k;k--)
{
for(j=2666;j;j--) ; //此处j不得小于27否则读不出数据
}
}
void int_twi_sccb()
{
TWBR=50;/*设置SCL的时钟频率在19。230根据公式SCL FREQUENCY=CPU Clock frequency/16+2(TWBR)*4的TWPS次方
*/
}
/**************************************************/
//本函数为设置7670寄存器的最底层操作函数,
//无返回值
//ADD是要写的寄存器地址,DATE是向里面写入的数据
//
/*************************************************/
void write_ov7670_sccb_twi(uchar add,uchar date)//向add指定的地址内写入指定的数据
{
//主发送模式程序/////////////////////////
TWCR=0xa4;//插入开始新号使能总线,使能START
delay1ms(1);
while(0==get_bit(TWCR,7));//检测起始信号是否发送?
while((TWSR&0xf8)!=0x08)
{
send_byte(0xf4);
send_byte(0x21);
send_byte(0xf4);//出错
}
send_byte(0xf4);
send_byte(0x11);
send_byte(0xf4);//START信号的正确响应码
TWDR=0x42;//写操作SCCB的器件地址0X42
TWCR=0x84;//重启启动数据的发送
delay1ms(1);
while(0==get_bit(TWCR,7));//检测数据信号是否发送?
while((TWSR&0XF8)!=0x18)
{
send_byte(0xf4);
send_byte(0x22);
send_byte(0xf4);
}
send_byte(0xf4);
send_byte(0x12);
send_byte(0xf4);//发送完毕有ACK应答响应码
TWDR=add;//写操作SCCB的寄存器子地址
TWCR=0x84;//重启启动数据的发送
delay1ms(1);
while(0==get_bit(TWCR,7));//检测数据信号是否发送?
while((TWSR&0XF8)!=0x28)
{
send_byte(0xf4);
send_byte(0x23);//出错
send_byte(0xf4);
}
send_byte(0xf4);
send_byte(0x13);
send_byte(0xf4);//发送完毕有ACK应答响应码
TWDR=date;//写操作SCCB的寄存器数据
TWCR=0x84;//重启启动数据的发送
delay1ms(1);
while(0==get_bit(TWCR,7));//检测数据信号是否发送?
while((TWSR&0XF8)!=0x28)
{
send_byte(0xf4);
send_byte(0x24);//出错
send_byte(0xf4);
}
send_byte(0xf4);
send_byte(0x14);
send_byte(0xf4);//发送完毕有ACK应答响应码
TWCR=0x94;//STOP信号
}
/**************************************************/
//本函数为设置7670寄存器的最底层操作函数,
//返回值为8位的数据
//ADD是要读的寄存器地址,
//
/*************************************************/
uchar read_ov7670_sccb_twi(uchar add)//读取一个add中的一个字节个数据
{
uchar date;
//主发送模式程序/////////////////////////
TWCR=0xa4;//插入开始新号使能总线,使能START
delay1ms(1);
while(0==get_bit(TWCR,7));//检测起始信号是否发送?
while((TWSR&0xf8)!=0x08)
{
send_byte(0xf4);
send_byte(0x01);
send_byte(0xf4);//出错
}
send_byte(0xf4);
send_byte(0x10);
send_byte(0xf4);//START信号的正确响应码
TWDR=0x42;//写操作SCCB的器件写地址0X42
TWCR=0x84;//重启启动数据的发送
delay1ms(1);
while(0==get_bit(TWCR,7));//检测数据信号是否发送?
while((TWSR&0XF8)!=0x18)
{
send_byte(0xf4);
send_byte(0x02);
send_byte(0xf4);
}
send_byte(0xf4);
send_byte(0x20);
send_byte(0xf4);//发送完毕有ACK应答响应码
TWDR=add;//写操作SCCB的寄存器子地址
TWCR=0x84;//重启启动数据的发送
delay1ms(1);
while(0==get_bit(TWCR,7));//检测数据信号是否发送?
while((TWSR&0XF8)!=0x28)
{
send_byte(0xf4);
send_byte(0x03);//出错
send_byte(0xf4);
}
send_byte(0xf4);
send_byte(0x30);
send_byte(0xf4);//发送完毕有ACK应答响应码
TWCR=0x94;//STOP信号
delay1ms(10);////无论读完还是模式转换后都要等待
//主接收模式程序///////////////////////////////////////////
TWCR=0xa4;//插入开始新号使能总线,使能START
delay1ms(1);
while(0==get_bit(TWCR,7));//检测起始信号是否发送?
while((TWSR&0XF8)!=0x08)
{send_byte(0xf4);
send_byte(0x04);//出错
send_byte(0xf4);
}
send_byte(0xf4);
send_byte(0x40);
send_byte(0xf4);//START信号的正确响应码
TWDR=0x43;//写操作SCCB的器件读地址0X43
TWCR=0x84;//重启启动数据的发送
delay1ms(1);
while(0==get_bit(TWCR,7));//检测数据信号是否发送?
while((TWSR&0XF8)!=0x40)
{send_byte(0xf4);
send_byte(0x05);//出错
send_byte(0xf4);
}
send_byte(0xf4);
send_byte(0x50);
send_byte(0xf4);//发送完毕有ACK应答响应码
TWCR=0x84;//启动数据的接收
delay1ms(1);
while(0==get_bit(TWCR,7));//检测数据信号是否发送?
date=TWDR;
while((TWSR&0XF8)!=0x58)
{send_byte(0xf4);
send_byte(0x06);
send_byte(0xf4);}//出错
send_byte(0xf4);
send_byte(0x60);
send_byte(0xf4);//发送完毕有NACK应答响应码
TWCR=0x94;//STOP信号
return(date);
}
void main()
{
uchar inceptdate;//读出的寄存器数据
DDRC=0xff;
PORTC=0xff;
USART_INT();
int_twi_sccb();
inceptdate=read_ov7670_sccb_twi(0x15);//读寄存器地址为15的数据
send_byte(0xf4);
send_byte(inceptdate);//数据显示
send_byte(0xf4);
delay1ms(10);//无论读完还是写完都要等等
write_ov7670_sccb_twi(0x15,0x20);//写寄存器15的数据位20
delay1ms(10);
inceptdate=read_ov7670_sccb_twi(0x15);//再次读15的数据
send_byte(0xf4);
send_byte(inceptdate);//送显示,已是校验写入钱与写入后的对比
send_byte(0xf4);
while(1);
}
复制代码
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1