📄 r_w.c
字号:
#include<reg51.h>
#include<stdio.h>
#include "i2c.h"
#define WRITE_6752 0x40 //定义6752的芯片写地址
#define READ_6752 0x41 //定义6752的芯片读地址
#define FALSE 0
#define TRUE 1
sbit RES_6752 = 0xA3;
sbit REDLED = 0xA1;
sbit BLUELED = 0xA0;
//以下定义saa6752五种工作模式转换命令
#define soft_reset 0x00
#define enable 0x01
#define start 0x02
#define stop 0x03
#define pause 0x04
#define reconfigure 0x05
#define sleep 0x06
#define forced_reconfigure 0x07
#define BYTE_SIZE 64
unsigned int data read_reg; //定义变量存储读得寄存器值
unsigned int data option; //定义选择标志
unsigned int data reg_addr; //定义寄存器子地址
unsigned int data write_reg; //定义写入寄存器值
unsigned int xdata reg_buffer[BYTE_SIZE]={0x00}; //定义外部数据缓冲区用于存储多字节写6752寄存器值
unsigned char code C2_buffer_PAT[26]={0,71,64,0,16,0,0,176,17,0,0,193,0,0,0,0,224,16,0,1,224,32,211,106,240,172}; //定义数组存储C2地址189个字节的数据
unsigned char code C2_buffer_PMT[44]={1,71,64,32,16,0,2,176,35,0,1,193,0,0,225,4,240,12,11,2,64,31,16,6,192,107,108,192,6,0,2,225,0,240,0,3,225,3,240,0,18,47,233,137};
unsigned char code C2_buffer_SIT[53]={4,71,64,31,16,0,127,240,44,255,255,193,0,0,240,10,99,8,192,146,124,255,255,255,255,255,0,1,192,19,72,17,7,7,80,104,105,108,105,112,115,7,69,77,80,82,69,83,83,83,94,18,58};
//定义6752模式转换命令函数,command_name为上面定义的八个命令之一
bit mode_transition(unsigned int command_name)
{
I_init();
I_start();
if(command_name<=0x07&&command_name>=0x00)
{
if(I_send(WRITE_6752))
return (I_send((unsigned char)command_name));
else
return ( FALSE );
}
else
{
printf("\ncommmand name error!\n");
return ( FALSE );
}
}
bit address_6752(unsigned int s_address) //向6752写入芯片从地址和片内字节地址
{ //写入成功则返回1,否则返回0
I_init();
I_start();
if(I_send(WRITE_6752))
return (I_send(s_address));
else
return (FALSE);
}
bit read_6752_single_byte(unsigned int subaddress) //对6752片内指定寄存器读取,subaddress为寄存器地址
{ //读得值存于read_reg中,读取成功返回1,否则返回0
if(address_6752((unsigned char)subaddress))
{
I_start();
if(I_send(READ_6752))
{
read_reg=I_receive();
return(TRUE);
}
else
{
I_stop();
return(FALSE);
}
}
else
I_stop();
return(FALSE);
}
bit read_6752_multi_byte(unsigned int subaddress, unsigned int byte_num )
{
int i;
if(address_6752((unsigned char)subaddress))
{
I_start();
if(I_send(READ_6752))
{
for( i=0; i<byte_num; i++)
{
reg_buffer[i]=( I_receive() );
if(i!=byte_num)
I_Ack();
else
{
I_clock();
I_stop();
}
}
return ( TRUE );
}
else
{
I_stop();
return ( FALSE );
}
}
else
I_stop();
return ( FALSE );
}
bit write_6752_single_byte(unsigned int subaddress, unsigned int write_value) //对6752片内寄存器写入值,subaddress为寄存器值
{ //write_value为写入值,写入成功返回1,否则返回0
if(address_6752((unsigned char)subaddress)&I_send((unsigned char)write_value))
I_stop();
else
return(FALSE);
return(TRUE);
}
bit write_6752_multi_byte( unsigned int subaddress, unsigned int byte_num)
{
int i;
if(address_6752((unsigned char)subaddress))
{
for( i=0; i<byte_num; i++)
{
if(I_send((unsigned char)reg_buffer[i]))
continue;
else
{
printf("write 6752 multi error! ");
return( FALSE );
}
}
I_stop();
}
return(TRUE);
}
bit write_6752_PAT(void)
{
int i;
if(address_6752(0xC2))
{
for( i=0; i<26; i++)
{
if(I_send(C2_buffer_PAT[i]))
continue;
else
{
printf("\nwrite 6752 PAT ERROR!\n");
return( FALSE );
}
}
I_stop();
}
else
return (FALSE);
return(TRUE);
}
bit write_6752_PMT(void)
{
int i;
if(address_6752(0xC2))
{
for( i=0; i<44; i++)
{
if(I_send(C2_buffer_PMT[i]))
continue;
else
{
printf("\nwrite 6752 PMT ERROR!\n");
return( FALSE );
}
}
I_stop();
}
else
return (FALSE);
return(TRUE);
}
bit write_6752_SIT(void)
{
int i;
if(address_6752(0xC2))
{
for( i=0; i<53; i++)
{
if(I_send(C2_buffer_SIT[i]))
continue;
else
{
printf("\nwrite 6752 SIT ERROR!\n");
return( FALSE );
}
}
I_stop();
}
else
return (FALSE);
return(TRUE);
}
void main()
{
unsigned int gl_tndb1,gl_tndb2,gl_tncb,gl_tnbb,gl_tnab;
unsigned int gl_ifb,gl_ifc,gl_ife;
unsigned int I;
unsigned int byte_number;
SCON = 0x52;
TMOD = 0x20;
TCON = 0x69;
TH1 = 0xfd;
delay_time(1000);
I_init();
REDLED = 1;
BLUELED = 1;
RES_7115=0;
RES_6752=0;
delay_time(1000);
//以下配置6752寄存器
RES_6752=1;
delay_time(1000);
if(mode_transition(soft_reset))
printf("\n reset SAA6752 success !");
else
printf("\n reset SAA6752 fail !");
read_6752_single_byte(0x10);
delay_time(1000);
if(read_6752_single_byte(0x10)) //get running mode
{
switch(read_reg)
{
case 1: printf("\nthe current mode is IDLE!");
break;
case 2: printf("\nthe current mode is ENCODING!");
break;
case 4: printf("\nthe current mode is STOPPED!");
break;
case 8: printf("\nthe current mode is PAUSED!");
break;
case 16: printf("\nthe current mode is RESERVED!");
break;
case 32: printf("\nthe current mode is BUSY FLAG!");
break;
default: printf("\nthe current mode is unkowned! x%", read_reg);
}
}
else
printf("\nread_6752_single_byte( 0x10 ) error!\n");
write_6752_single_byte( 0x08, 0x00);
reg_buffer[0]=0x00;
reg_buffer[1]=0x00;
write_6752_multi_byte( 0x11, 2);
write_6752_single_byte( 0x20, 0x02);
write_6752_single_byte( 0x30, 0x01);
write_6752_single_byte( 0x31, 0x00);
write_6752_single_byte( 0x32, 0x00);
write_6752_single_byte( 0x33, 0x01);
write_6752_single_byte( 0x34, 0x00);
write_6752_single_byte( 0x40, 0x00);
write_6752_single_byte( 0x41, 0x00);
write_6752_single_byte( 0x42, 0x00);
write_6752_single_byte( 0x43, 0x00);
reg_buffer[0]=0x00;
reg_buffer[1]=0x00;
write_6752_multi_byte( 0x44, 2);
reg_buffer[0]=0x00;
reg_buffer[1]=0x00;
write_6752_multi_byte( 0x45, 2);
write_6752_single_byte( 0x46, 0x00);
write_6752_single_byte( 0x47, 0x00);
write_6752_single_byte( 0x50, 0x00);
reg_buffer[0]=0x33;
reg_buffer[1]=0x20;
reg_buffer[2]=0x16;
reg_buffer[3]=0x0d;
write_6752_multi_byte( 0x51, 4);
reg_buffer[0]=0x0A;
reg_buffer[1]=0x0F;
reg_buffer[2]=0x14;
write_6752_multi_byte( 0x52, 3);
write_6752_single_byte( 0x53, 0x00);
reg_buffer[0]=0x00;
reg_buffer[1]=0x01;
reg_buffer[2]=0x00;
reg_buffer[3]=0x00;
write_6752_multi_byte( 0x54, 16);
write_6752_single_byte( 0x55, 0x08);
write_6752_single_byte( 0x61, 0x00);
write_6752_single_byte( 0x63, 0x00);
write_6752_single_byte( 0x65, 0x00);
write_6752_single_byte( 0x70, 0x00);
write_6752_single_byte( 0x71, 0x01);
reg_buffer[0]=0x03;
reg_buffer[1]=0x0a;
write_6752_multi_byte( 0x72, 2);
reg_buffer[0]=0x00;
reg_buffer[1]=0x00;
write_6752_multi_byte( 0x73, 64);
write_6752_single_byte( 0x74, 0x00);
write_6752_single_byte( 0x75, 0x00);
write_6752_multi_byte( 0x76, 64);
reg_buffer[0]=0x00;
reg_buffer[1]=0x00;
write_6752_multi_byte( 0x77, 2);
write_6752_single_byte( 0x78, 0x20);
write_6752_single_byte( 0x7B, 0x00);
write_6752_single_byte( 0x7C, 0x00);
write_6752_single_byte( 0x7D, 0x00);
reg_buffer[0]=0x07;
reg_buffer[1]=0xd0;
write_6752_multi_byte( 0x80, 2);
reg_buffer[0]=0x0f;
reg_buffer[1]=0xa0;
write_6752_multi_byte( 0x81, 2);
write_6752_single_byte( 0x82, 0x04);
write_6752_single_byte( 0x83, 0x0C);
write_6752_single_byte( 0x84, 0x80);
write_6752_single_byte( 0x90, 0x00);
write_6752_single_byte( 0x91, 0x00);
write_6752_single_byte( 0x92, 0x00);
write_6752_single_byte( 0x93, 0x00);
write_6752_single_byte( 0x94, 0x00);
write_6752_single_byte( 0x95, 0x00);
write_6752_single_byte( 0x96, 0x00);
write_6752_single_byte( 0xA4, 0x00);
write_6752_single_byte( 0xB0, 0x05);
reg_buffer[0]=0x12;
reg_buffer[1]=0xc0;
write_6752_multi_byte( 0xB1, 2);
reg_buffer[0]=0x00;
reg_buffer[1]=0x00;
write_6752_multi_byte( 0xB2, 2);
write_6752_single_byte( 0xB3, 0xE0);
write_6752_single_byte( 0xB4, 0xC0);
write_6752_single_byte( 0xB5, 0x00);
write_6752_single_byte( 0xB6, 0x12);
write_6752_single_byte( 0xB7, 0x00);
write_6752_single_byte( 0xB9, 0x00);
reg_buffer[0]=0x01;
reg_buffer[1]=0x00;
write_6752_multi_byte(0xC0,2);
reg_buffer[0]=0x01;
reg_buffer[1]=0x03;
write_6752_multi_byte(0xC1,2);
write_6752_PAT();
write_6752_PMT();
write_6752_SIT();
write_6752_single_byte(0xC3,0x00);
reg_buffer[0]=0x01;
reg_buffer[1]=0x04;
write_6752_multi_byte(0xC4,2);
write_6752_single_byte(0xD0,0x01);
reg_buffer[0]=0x00;
reg_buffer[1]=0x00;
write_6752_multi_byte(0xF6,2);
while(1)
{
REDLED = ~REDLED;
printf("\nplease select 1,2,3,4,5,6,7,8,9,10:\n");
printf(" 1:write the FQ1216 Tuner Part.\n");
printf(" 2:Read the FQ1216 Tuner Part.\n");
printf(" 3:write the FQ1216 IF Part.\n");
printf(" 4:Read the FQ1216 IF Part.\n");
printf(" 5:Read the single register in saa6752.\n");
printf(" 6:write the single register in saa6752.\n");
printf(" 7:Read the single register in saa7115.\n");
printf(" 8:write the single register in saa7115.\n");
printf(" 9:Read the multi-byte register in saa6752.\n");
printf(" 10:mode transtion to saa6752: \n");
scanf("%u",&option);
if(option==1)
{
printf("\nplease input db1:\n");
scanf("%x",&gl_tndb1);
printf("\nplease input db2:\n");
scanf("%x",&gl_tndb2);
printf("\nplease input cb:\n");
scanf("%x",&gl_tncb);
printf("\nplease input bb:\n");
scanf("%x",&gl_tnbb);
printf("\nplease input ab:\n");
scanf("%x",&gl_tnab);
write_tuner(gl_tndb1,gl_tndb2,gl_tncb,gl_tnbb,gl_tnab);
}
else if(option==2)
{
read_tuner();
}
else if (option==3)
{
printf("\nplease input b:\n");
scanf("%x",&gl_ifb);
printf("\nplease input c:\n");
scanf("%x",&gl_ifc);
printf("\nplease input e:\n");
scanf("%x",&gl_ife);
write_IF(gl_ifb,gl_ifc,gl_ife);
}
else if (option==4)
{
read_IF();
}
else if(option==5)
{
printf("\nplease input the register address you wanna read:\n");
scanf("%x",®_addr);
if(read_6752_single_byte(reg_addr))
printf("\nsuccess and the register value is: %x ",read_reg);
else
printf("\nfail to read saa6752.");
}
else if(option==6)
{
printf("\nplease input the register address you wanna write:\n");
scanf("%x",®_addr);
printf("\nplease input the register value you wanna write:\n");
scanf("%x",&write_reg);
if(write_6752_single_byte(reg_addr,write_reg))
printf("\nsuccess to write saa6752.\n");
else
printf("fail to write saa6752.\n");
}
else if(option==10)
{
printf("\nplease input the mode transition:\n");
scanf("%x",®_addr);
if(mode_transition(reg_addr))
printf("transition success!");
else printf("transition error!");
}
else if(option==9)
{
printf("\nplease input the register address you wanna read:\n");
scanf("%x",®_addr);
printf("\nplease input the register byte number:\n");
scanf("%u",&byte_number);
if(read_6752_multi_byte( reg_addr , byte_number ))
printf("read saa6752 success and the register value is:\n");
for(I=0;I<byte_number;I++)
printf(" %x \n", reg_buffer[I]);
}
else if(option==7)
{
printf("\nplease input the register address you wanna read:\n");
scanf("%x",®_addr);
if(read_7115_single_byte(reg_addr))
printf("\nsuccess and the register value is: %x ",read_reg);
else
printf("\nfail to read saa7115.\n");
}
else if(option==8)
{
printf("\nplease input the register address you wanna write:\n");
scanf("%x",®_addr);
printf("\nplease input the register value you wanna write:\n");
scanf("%x",&write_reg);
if(write_7115_single_byte(reg_addr,write_reg))
printf("\nsuccess to write saa7115.\n");
else
printf("\nfail to write saa7115.\n");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -