example_280x_h452.c
来自「DSP2808例程。TMS320F2808DSP的各个模块的应用例程」· C语言 代码 · 共 429 行
C
429 行
// TI File $Revision: /main/3 $
// Checkin $Date: December 3, 2004 13:40:48 $
//###########################################################################
//
// FILE: Example_280xI2c_h452.c
//
// TITLE: DSP280x I2C EEPROM Example
//
// ASSUMPTIONS:
//
// This program requires the DSP280x header files.
//
// This program requires an external I2C EEPROM connected to
// the I2C bus at address 0x50.
//
// As supplied, this project is configured for "boot to SARAM"
// operation. The 280x Boot Mode table is shown below.
// For information on configuring the boot mode of an eZdsp,
// please refer to the documentation included with the eZdsp,
//
// Boot GPIO18 GPIO29 GPIO34
// Mode SPICLKA SCITXDA
// SCITXB
// -------------------------------------
// Flash 1 1 1
// SCI-A 1 1 0
// SPI-A 1 0 1
// I2C-A 1 0 0
// ECAN-A 0 1 1
// SARAM 0 1 0 <- "boot to SARAM"
// OTP 0 0 1
// I/0 0 0 0
//
// DESCRIPTION:
//
// This program will write 1-14 words to EEPROM and read them back.
// The data written and the EEPROM address written to are contained
// in the message structure, I2cMsgOut1. The data read back will be
// contained in the message structure I2cMsgIn1.
//
// This program will work with the on-board I2C EEPROM supplied on
// the F280x eZdsp.
//
//
//###########################################################################
// Original Author: D.F.
//
// $TI Release: DSP280x V1.30 $
// $Release Date: February 10, 2006 $
//###########################################################################
#include "DSP280x_Device.h" // DSP280x Headerfile Include File
#include "DSP280x_Examples.h" // DSP280x Examples Include File
/* 2线接口的位操作,与单片机有关 */
#define CH452_SCL_SET GpioDataRegs.GPBSET.bit.GPIO33 = 1//{CH452_SCL=1;}
#define CH452_SCL_CLR GpioDataRegs.GPBCLEAR.bit.GPIO33 = 1//{CH452_SCL=0;}
#define CH452_SDA_SET GpioDataRegs.GPBSET.bit.GPIO32 = 1//{CH452_SDA=1;}
#define CH452_SDA_CLR GpioDataRegs.GPBCLEAR.bit.GPIO32 = 1//{CH452_SDA=0;}
#define CH452_SDA_IN GpioDataRegs.GPBDAT.bit.GPIO32//(CH452_SDA)
#define CH452_SDA_D_OUT GpioCtrlRegs.GPBDIR.bit.GPIO32 = 1; // 设置SDA为输出方向,对于双向I/O需切换为输出
#define CH452_SDA_D_IN GpioCtrlRegs.GPBDIR.bit.GPIO32 = 0; //{CH452_SDA=1;} // 设置SDA为输入方向,对于双向I/O需切换为输入
/* CH451和CH452的常用命令码 */
#define CH452_NOP 0x0000 // 空操作
#define CH452_RESET 0x0201 // 复位
#define CH452_LEVEL 0x0100 // 加载光柱值
#define CH452_CLR_BIT 0x0180 // 段位清0
#define CH452_SET_BIT 0x01C0 // 段位置1
#define CH452_SLEEP 0x0202 // 进入睡眠状态
#define CH452_LEFTMOV 0x0300 //设置移动方式-作移
#define CH452_LEFTCYC 0x0301 //设置移动方式-左循
#define CH452_RIGHTMOV 0x0302 //设置移动方式-右移
#define CH452_RIGHTCYC 0x0303 //设置移动方式-右循
#define CH452_SELF_BCD 0x0380 //自定义BCD码
#define CH452_SYSOFF 0x0400 //关显示、键盘
#define CH452_SYSON1 0x0401 //开显示
#define CH452_SYSON2 0x0403 //开显示、键盘
#define CH452_SYSON2W 0x0423 //开显示、键盘, 真正2线接口
#define CH452_DSP 0x0500 //设置默认显示方式
#define CH452_BCD 0x0580 //设置BCD译码方式
#define CH452_TWINKLE 0x0600 //设置闪烁控制
#define CH452_GET_KEY 0x0700 // 获取按键
#define CH452_DIG0 0x0800 //数码管位0显示
#define CH452_DIG1 0x0900 //数码管位1显示
#define CH452_DIG2 0x0a00 //数码管位2显示
#define CH452_DIG3 0x0b00 //数码管位3显示
#define CH452_DIG4 0x0c00 //数码管位4显示
#define CH452_DIG5 0x0d00 //数码管位5显示
#define CH452_DIG6 0x0e00 //数码管位6显示
#define CH452_DIG7 0x0f00 //数码管位7显示
// Note: I2C Macros used in this example can be found in the
// DSP280x_I2C_defines.h file
// Prototype statements for functions found within this file.
void I2CA_Init(void);
void CH452_Write(unsigned char);
unsigned char CH452_Read(void);
void CH452_I2c_Start();
void CH452_I2c_Stop();
void CH452_I2c_WrByte(unsigned char);
unsigned char CH452_I2c_RdByte();
interrupt void read_key_isr(void);
#define I2C_SLAVE_ADDR 0x50
#define I2C_NUMBYTES 4
#define I2C_EEPROM_HIGH_ADDR 0x00
#define I2C_EEPROM_LOW_ADDR 0x30
Uint16 temp = 8;
void DELAY_1US()
{Uint16 i;
for(i=0;i<1000;i++)
{
}
}
void main(void)
{
//Uint16 Error;
// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the DSP280x_SysCtrl.c file.
InitSysCtrl();
// Step 2. Initalize GPIO:
// This example function is found in the DSP280x_Gpio.c file and
// illustrates how to set the GPIO to it's default state.
// InitGpio();
// Setup only the GP I/O only for I2C functionality
InitI2CGpio();
// Step 3. Clear all interrupts and initialize PIE vector table:
// Disable CPU interrupts
DINT;
// Initialize PIE control registers to their default state.
// The default state is all PIE interrupts disabled and flags
// are cleared.
// This function is found in the DSP280x_PieCtrl.c file.
InitPieCtrl();
// Disable CPU interrupts and clear all CPU interrupt flags:
IER = 0x0000;
IFR = 0x0000;
// Initialize the PIE vector table with pointers to the shell Interrupt
// Service Routines (ISR).
// This will populate the entire table, even if the interrupt
// is not used in this example. This is useful for debug purposes.
// The shell ISR routines are found in DSP280x_DefaultIsr.c.
// This function is found in DSP280x_PieVect.c.
InitPieVectTable();
// 初始化XINT
// Make GPIO27 the input source for XINT1
EALLOW;
GpioCtrlRegs.GPAMUX1.bit.GPIO15 = 0; // GPIO27 = GPIO27
GpioCtrlRegs.GPADIR.bit.GPIO15 = 0; // GPIO27 = input
GpioCtrlRegs.GPAQSEL1.bit.GPIO15 = 0; // xint1 Synch to SYSCLKOUT only
//GpioCtrlRegs.GPAQSEL2.bit.GPIO27 = 2; // XINT2 Qual using 6 samples
//GpioCtrlRegs.GPACTRL.bit.QUALPRD0= 0xFF;//Each sampling windows is 510*sys
GpioIntRegs.GPIOXINT1SEL.all = 15; // XINT1 connected to GPIO27
EDIS;
// Interrupts that are used in this example are re-mapped to
// ISR functions found within this file.
EALLOW; // This is needed to write to EALLOW protected registers
//PieVectTable.I2CINT1A = &i2c_int1a_isr;
PieVectTable.XINT1 = &read_key_isr;
EDIS; // This is needed to disable write to EALLOW protected registers
// Step 4. Initialize all the Device Peripherals:
// This function is found in DSP280x_InitPeripherals.c
// InitPeripherals(); // Not required for this example
//I2CA_Init();
// Step 5. User specific code
// Enable interrupts required for this example
// Enable I2C interrupt 1 in the PIE: Group 8 interrupt 1
PieCtrlRegs.PIECTRL.bit.ENPIE = 1; //test enable the PIE block
PieCtrlRegs.PIEIER1.bit.INTx4 = 1;
XIntruptRegs.XINT1CR.bit.POLARITY = 0; //falling edge
XIntruptRegs.XINT1CR.bit.ENABLE = 1; //enable xint1
// Enable CPU INT8 which is connected to PIE group 8
//IER |= M_INT8;
IER |= M_INT1;
EINT;//Enable Global interrupt INTM
ERTM;//Enable Global realtime interrupt DBGM
CH452_Write(CH452_SYSON2); //两线制方式,如果SDA用做按键中断输出,那么命令应该为(0x04,0x23)
DELAY_1US();
CH452_Write(CH452_BCD); // BCD译码,8个数码管
DELAY_1US();
CH452_Write(CH452_DIG0 | temp); // 显示字符8
DELAY_1US();
CH452_Write(CH452_DIG1 | temp);
DELAY_1US();
CH452_Write(CH452_DIG2 | temp);
DELAY_1US();
CH452_Write(CH452_DIG3 | temp);
DELAY_1US();
CH452_Write(CH452_DIG4 | temp);
DELAY_1US();
CH452_Write(CH452_DIG5 | temp);
DELAY_1US();
CH452_Write(CH452_DIG6 | temp);
DELAY_1US();
CH452_Write(CH452_DIG7 | temp);
DELAY_1US();
//CH452_SDA_SET;
//CH452_SDA_CLR;
//CH452_SCL_SET;
//CH452_SCL_CLR;
// Application loop
for(;;)
{
DELAY_1US();
CH452_Write(CH452_DIG0 | temp); // 显示字符8
DELAY_1US();
CH452_Write(CH452_DIG1 | temp);
DELAY_1US();
CH452_Write(CH452_DIG2 | temp);
DELAY_1US();
CH452_Write(CH452_DIG3 | temp);
DELAY_1US();
CH452_Write(CH452_DIG4 | temp);
DELAY_1US();
CH452_Write(CH452_DIG5 | temp);
DELAY_1US();
CH452_Write(CH452_DIG6 | temp);
DELAY_1US();
CH452_Write(CH452_DIG7 | temp);
DELAY_1US();
DELAY_1US();
} // end of for(;;)
} // end of main
interrupt void read_key_isr(void)
{unsigned int key;
key = CH452_Read();
temp = key-120 ;
PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
}
void CH452_I2c_Start()
{
//#ifdef USE_KEY
// DISABLE_INTERRUPT; //禁止键盘中断,防止开始时被CH452中断而进入中断服务程序中的START
//#endif
CH452_SDA_SET; /*发送起始条件的数据信号*/
//CH452_SDA_D_OUT; /* 设置SDA为输出方向 */
EALLOW;
GpioCtrlRegs.GPBDIR.bit.GPIO32 = 1;
EDIS;
CH452_SCL_SET;
//DELAY_1US;
DELAY_1US();
CH452_SDA_CLR; /*发送起始信号*/
//DELAY_1US;
DELAY_1US();
CH452_SCL_CLR; /*钳住I2C总线,准备发送或接收数据 */
//DELAY_1US;
DELAY_1US();
}
void CH452_I2c_Stop()
{
CH452_SDA_CLR;
//CH452_SDA_D_OUT; /* 设置SDA为输出方向 */
EALLOW;
GpioCtrlRegs.GPBDIR.bit.GPIO32 = 1;
EDIS;
//DELAY_1US;
DELAY_1US();
CH452_SCL_SET;
//DELAY_1US;
DELAY_1US();
CH452_SDA_SET; /*发送I2C总线结束信号*/
//DELAY_1US;
DELAY_1US();
//CH452_SDA_D_IN; /* 设置SDA为输入方向 */
//#ifdef USE_KEY
// ENABLE_INTERRUPT;
//#endif
}
void CH452_Write(unsigned char command) //写命令
{
CH452_I2c_Start(); /*启动总线*/
#ifdef ENABLE_2_CH452 // 若有两个CH452并连
CH452_I2c_WrByte(((unsigned char)(command>>7)&0x3E)|0x40); // CH452的ADDR=0时
#else
CH452_I2c_WrByte(((unsigned char)(command>>7)&0x3E)|0x60); // CH452的ADDR=1时(默认)
#endif
//
// DELAY_1US();
//CH452_SDA_SET;
//DELAY_1US;
// DELAY_1US();
//CH452_SCL_SET;
//DELAY_1US;
// DELAY_1US();
//CH452_SCL_CLR;
//DELAY_1US;
DELAY_1US();
//
CH452_I2c_WrByte((unsigned char)command); /*发送数据*/
CH452_I2c_Stop(); /*结束总线*/
}
unsigned char CH452_Read() //读取按键
{
unsigned char key;
CH452_I2c_Start(); /*启动总线*/
CH452_I2c_WrByte(0x6F); // 若有两个CH452并连,当ADDR=0时,此值为0x4F
key=CH452_I2c_RdByte(); /*读取数据*/
CH452_I2c_Stop(); /*结束总线*/
return(key);
}
void CH452_I2c_WrByte(unsigned char c) //写一个字节数据
{
unsigned char i;
//CH452_SDA_D_OUT; /* 设置SDA为输出方向 */
EALLOW;
GpioCtrlRegs.GPBDIR.bit.GPIO32 = 1;
EDIS;
for(i=0;i!=8;i++) // 输出8位数据
{
if(c&0x80) {CH452_SDA_SET;}
else {CH452_SDA_CLR;}
//DELAY_1US;
DELAY_1US();
CH452_SCL_SET;
c<<=1;
//DELAY_1US;
DELAY_1US();
CH452_SCL_CLR;
DELAY_1US();
}
//CH452_SDA_D_IN; /* 设置SDA为输入方向 */
EALLOW;
GpioCtrlRegs.GPBDIR.bit.GPIO32 = 0;
EDIS;
CH452_SDA_SET;
//DELAY_1US;
DELAY_1US();
CH452_SCL_SET; // 接收应答
//DELAY_1US;
DELAY_1US();
CH452_SCL_CLR;
//DELAY_1US;
DELAY_1US();
}
unsigned char CH452_I2c_RdByte() //读一个字节数据
{
unsigned char c,i;
//CH452_SDA_D_IN; /* 设置SDA为输入方向 */
EALLOW;
GpioCtrlRegs.GPBDIR.bit.GPIO32 = 0;
EDIS;
CH452_SDA_SET;
DELAY_1US();
c=0;
for(i=0;i!=8;i++) // 输入8位数据
{
CH452_SCL_SET;
//DELAY_1US;
DELAY_1US();
c<<=1;
if(CH452_SDA_IN) c|=0x01; // 输入1位
CH452_SCL_CLR;
//DELAY_1US;
DELAY_1US();
}
CH452_SDA_SET;
DELAY_1US();
CH452_SCL_SET; // 发出无效应答
//DELAY_1US;
DELAY_1US();
CH452_SCL_CLR;
//DELAY_1US;
DELAY_1US();
return(c);
}
void InitI2CGpio()
{
EALLOW;
GpioCtrlRegs.GPBPUD.bit.GPIO32 = 0; // Enable pullup on GPIO34
GpioCtrlRegs.GPBMUX1.bit.GPIO32 = 0; // GPIO32 = GPIO32
GpioCtrlRegs.GPBDIR.bit.GPIO32 = 1; // GPIO32 = output
GpioDataRegs.GPBSET.bit.GPIO32 = 1;
GpioCtrlRegs.GPBPUD.bit.GPIO33 = 0; // Enable pullup on GPIO34
GpioCtrlRegs.GPBMUX1.bit.GPIO33 = 0; // GPIO33 = GPIO33
GpioCtrlRegs.GPBDIR.bit.GPIO33 = 1; // GPIO33 = input
GpioDataRegs.GPBSET.bit.GPIO33 = 1; //
EDIS;
}
//===========================================================================
// No more.
//===========================================================================
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?