⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 C code for using 8051 to control philiphs I2C FM tuner (use keil c compiler)
💻 C
字号:
/* giantbicycle */
#include "at89s52.h"

#define M_EN 	0	/* Multi-channel Enable */

/* GPIO Pin Definitions */

//#define I2C_SCL_CLR	(P0 &= 0xFE)	/* Clear I2C SCL (P0.0)*/
#define I2C_SCL_CLR 		P0_0 = 0
//#define I2C_SCL_SET	(P0 |= 0x01)	/* Set I2C_SCL */
#define I2C_SCL_SET 		P0_0 = 1
//#define I2C_SDA_GET	P0 & 0x02	/* Read I2C SDA (P0.1) */
#define I2C_SDA_SDA_GET		P0_1
//#define I2C_SDA_CLR	(P0 &= 0xFD)	/* Clear I2C SDA */
#define I2C_SDA_CLR			P0_1 = 0
//#define I2C_SDA_SET	(P0 |= 0x02)	/* Set I2C_SDA */
#define I2C_SDA_SET			P0_1 = 1

/* I2C Data flow Direction Definitions */
#define I2C_WRITE	0x00	/* Write Direction */
#define I2C_READ	0x01	/* Read Direction */

/* I2C Acknowledge Definitions */
#define I2C_ACK		0
#define I2C_NO_ACK	1

/* FQ1236-MK3 Module Definitions */
#define TUMER_I2C_ADDRESS	0xC2	/* AS_TU pin (PIN6) is left floating */
#define IF_I2C_ADDRESS		0x86	/* AS_IF pin (PIN10) is open */

/* I2C functions */
/* Generate the start condition */
void I2C_Start();
/* Generate the stop condition */
void I2C_Stop();
/* Master send a byte to the slave device */
void I2C_Send_Byte(unsigned char byte);
/* Master send the slave address */
void I2C_Send_Addr(unsigned char addr, unsigned char dir);
/* Master send the data to the slave device */
#define I2C_Send_data	I2C_Send_Byte
/* Master get a byte from the slave device */
//unsigned char I2C_Get_Byte();
/* Master get the data from the slave device */
//unsigned char I2C_Get_Data();
/* Hold the voltage level of the signal */
data unsigned char counter;
void	I2C_NOP();

/* Tuner functions */
/* Tuner Section Programming (Write Mode) */
void tuner_write();
/* Tuner data array */
data unsigned char tuner_data[5]; /* |DB1|DB2|CB|BB|AB| */

/* Generate the start condition */
void I2C_Start()
{
	I2C_SDA_SET;
	I2C_SCL_SET;
	I2C_NOP();
	I2C_SDA_CLR;
	I2C_NOP();
	I2C_SCL_CLR;
	I2C_NOP();
}

/* Generate the stop condition */
void I2C_Stop()
{
	I2C_SCL_CLR;
	I2C_SDA_CLR;
	I2C_NOP();
	I2C_SCL_SET;
	I2C_NOP();
	I2C_SDA_SET;
	I2C_NOP();
}

/* Master send a byte to the slave device */
void I2C_Send_Byte(unsigned char byte)
{
	unsigned char mask;

	/* Send the byte from MSB to LSB */
	for (mask = 0x80 ; mask > 0 ; mask = mask >> 1 )
	{
		if (byte & mask)
			I2C_SDA_SET;
		else
			I2C_SDA_CLR;

		I2C_SCL_SET;
		I2C_NOP();
		I2C_SCL_CLR;
	}

	/* Check the Acknowledge from slave device */
	I2C_SDA_SET;
	I2C_SCL_SET;
	I2C_NOP();
	I2C_SCL_CLR;
	I2C_NOP();
}

/* Master send the slave address */
void I2C_Send_Addr(unsigned char addr, unsigned char dir)
{
	I2C_Send_Byte(addr + dir);
}
/* Master get a byte from the slave device */
#if 0
unsigned char I2C_Get_Byte()
{
	unsigned char mask;
	unsigned char retVal;

	I2C_SDA_SET;	/* configure the pin for input */
	retVal 	= 0;	/* initial the return value */

	for (mask = 0x80 ; mask != 0 ; mask = mask >> 1)
	{
		I2C_NOP();
		I2C_SCL_SET;
		if(I2C_SDA_GET)
			retVal = retVal | 1;
		I2C_NOP();
		I2C_SCL_CLR;
	}

	I2C_SDA_SET; /* No Acknowledge */
	I2C_NOP();
	I2C_SCL_SET;
	I2C_NOP();
	I2C_SCL_CLR;

	return retVal;
}
#endif
/* Master get the data from the slave device */
#if 0
unsigned char I2C_Get_Data()
{
	return I2C_Get_Byte();
}
#endif

/* Hold the voltage level of the signal */
void I2C_NOP()
{
	counter = 0;
}

/* Tuner Section Programming (Write Mode) */
/* |S|ADB(W)|A|DB1|A|DB2|A|CB|A|BB|A|AB|A|P| */
void tuner_write()
{
	I2C_Start();
	I2C_Send_Addr(TUMER_I2C_ADDRESS, I2C_WRITE);
	I2C_Send_data(tuner_data[0]);	/* DB1 */
	I2C_Send_data(tuner_data[1]);	/* DB2 */
	I2C_Send_data(tuner_data[2]);	/* CB  */
	I2C_Send_data(tuner_data[3]);	/* BB  */
	I2C_Send_data(tuner_data[4]);	/* AB  */
	I2C_Stop();
}

void main ()
{	
	unsigned char i;
#if M_EN
	unsigned char j;
#endif
	unsigned char channel = 11;	

	while(1) {
		switch (channel) {
	
		/*
			NTSC USA, fVIF = 45.75MHz, fss = 62.5KHz
	         	Test Cable Channel 7, fRF = 175.25MHz, TV Mid Band
		*/
#if M_EN
		case 7:
			tuner_data[0] = 0x0D; // DB1
			tuner_data[1] = 0xD0; // DB2
			tuner_data[2] = 0xCE; // CB
			tuner_data[3] = 0x02; // BB
			tuner_data[4] = 0x20; // AB
#endif
#if M_EN
			channel = 11;
#endif
		break;
        
       		/*
			Test Cable Channel 11, fRF = 199.25MHz, TV Mid Band
		*/
		case 11:
			tuner_data[0] = 0x0F; // DB1
			tuner_data[1] = 0x50; // DB2
			tuner_data[2] = 0xCE; // CB
			tuner_data[3] = 0x02; // BB
			tuner_data[4] = 0x20; // AB
#if M_EN
			channel = 5;
#endif
		break;

		/*
			Test Cable Channel 5 , fRF = 77.25MHz, TV Low Band
			(77.25 + 45.75) *  1000 / 62.5
		*/
#if M_EN
		case 5:
			tuner_data[0] = 0x07; // DB1
			tuner_data[1] = 0xB0; // DB2
			tuner_data[2] = 0xCE; // CB
			tuner_data[3] = 0x01; // BB
			tuner_data[4] = 0x20; // AB
#endif
#if M_EN
			channel = 9;
#endif
		break;
	
		/*
			Test Cable Channel 9 , fRF = 187.25MHz, TV Mid Band
			(187.25 + 45.75) *  1000 / 62.5
		*/
#if M_EN
		case 9:
			tuner_data[0] = 0x0E; // DB1
			tuner_data[1] = 0x90; // DB2
			tuner_data[2] = 0xCE; // CB
			tuner_data[3] = 0x02; // BB
			tuner_data[4] = 0x20; // AB
#endif
#if M_EN
			channel = 7;
#endif
		break;

		}

		tuner_write();

		for (i = 0 ; i < 50 ; i++)
			I2C_NOP();

		tuner_write();
#if M_EN
		for (i = 0 ; i < 255 ; i++)
			for (j = 0 ; j < 255; j++)
				I2C_NOP();
#endif
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -