/***************COPYRIGHT2011***************************
*File Name :ch450.c
*Author :gaochs
*Description :stm32控制ch450
stm32IO口分配:
SDA = PB11 SCL = PB10 INT = PE0
微調旋鈕(INT2_L = PB8 INT3_R = PB9)
蜂鳴器 = PE1
*Vision :V1.0.0
*Last Modify:
*--------------------------------------------------------
*Modification History
*********************************************************/
#include "stm32f4xx.h"
#include "CH450.h"
/*********************************************************
Variable Name :CH450_xxx
Dscription :define CH450 IO
**********************************************************/
#define CH450_SDA_SET GPIO_SetBits(GPIOE, GPIO_Pin_5) //SDA置高
#define CH450_SDA_CLR GPIO_ResetBits(GPIOE, GPIO_Pin_5) //SDA置低
#define CH450_SCL_SET GPIO_SetBits(GPIOE, GPIO_Pin_6) //SCL置高
#define CH450_SCL_CLR GPIO_ResetBits(GPIOE, GPIO_Pin_6) //SCL置低
#define CH450_RD_SDA GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_5)
#define DELAY mDelayuS(1000)
#define CH450_I2C_ADDR0 0x40 // CH450的ADDR=0時的地址
#define CH450_I2C_MASK 0x3E // CH450的2線接口高字節命令掩碼
#define CH450_GET_KEY 0x0700 // 獲取按鍵,返回按鍵代碼
// *******************
// 類型定義
// *******************
// **************************************
// CH450的全局變量End
// **************************************
void CH450_SDA_IN()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);
}
void CH450_SDA_OUT()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);
}
void CH450_SCL_OUT()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);
}
/***********************************************
*Function Name :mDelayuS
*Description :0.04us
*Input :n 延時時間
*Output :None
*Return :None
************************************************/
void mDelayuS(u16 n)
{
while( --n )
{
;
}
}
/***********************************************
*Function Name :mDelaymS
*Description :43us
*Input :delay 延時時間
*Output :None
*Return :None
************************************************/
void mDelaymS(u16 delay)
{
u16 ms;
u16 sm;
u16 c;
for(ms = delay; ms != 0; ms--)
{
for (sm = 200; sm != 0; sm--) c += 3;
for (sm = 200; sm != 0; sm--) c += 3;
for (sm = 200; sm != 0; sm--) c += 3;
for (sm = 200; sm != 0; sm--) c += 3;
for (sm = 240; sm != 0; sm--) c += 3;
}
for (ms = delay; ms != 0; ms--)
{
for (sm = 200; sm != 0; sm--) c += 3;
for (sm = 200; sm != 0; sm--) c += 3;
for (sm = 200; sm != 0; sm--) c += 3;
for (sm = 200; sm != 0; sm--) c += 3;
for (sm = 240; sm != 0; sm--) c += 3;
}
}
/***********************************************
*Function Name :CH450_I2c_Start
*Description :I2C START
*Input :None
*Output :None
*Return :None
************************************************/
void CH450_I2c_Start( void )
{
CH450_SDA_OUT(); /* 設置SDA為輸出方向 */
DELAY;
CH450_SDA_SET; /*發送起始條件的數據信號*/
DELAY;
CH450_SCL_SET;
DELAY;
CH450_SDA_CLR; /*發送起始信號*/
DELAY;
CH450_SCL_CLR; /*鉗住I2C總線,準備發送或接收數據 */
DELAY;
}
/***********************************************
*Function Name :CH450_I2c_Stop
*Description :I2C STOP
*Input :None
*Output :None
*Return :None
************************************************/
void CH450_I2c_Stop( void )
{
CH450_SDA_OUT(); /* 設置SDA為輸出方向 */
DELAY;
CH450_SDA_CLR;
DELAY;
CH450_SCL_SET;
DELAY;
CH450_SDA_SET; /*發送I2C總線結束信號*/
DELAY;
}
/***********************************************
*Function Name :CH450_I2c_WrByte
*Description :I2C 寫一個字節
*Input :c 待寫字節數據
*Output :None
*Return :None
************************************************/
void CH450_I2c_WrByte(u8 c) //寫一個字節數據
{
u8 i;
CH450_SDA_OUT(); /* 設置SDA為輸出方向 */
DELAY;
for (i = 0; i != 8; i++)
{
if (c & 0x80)
{
CH450_SDA_SET;
}
else
{
CH450_SDA_CLR;
}
DELAY;
CH450_SCL_SET;
c <<= 1;
DELAY;
CH450_SCL_CLR;
DELAY;
}
CH450_SDA_IN(); /* 設置SDA為輸入方向 */
DELAY;
CH450_SCL_SET; //接收應答
DELAY;
CH450_SCL_CLR;
DELAY;
}
void CH450_I2C_SendACK()
{
CH450_SDA_OUT();
CH450_SCL_CLR;
DELAY;
CH450_SDA_SET;
DELAY;
CH450_SCL_SET;
DELAY;
CH450_SCL_CLR;
DELAY;
CH450_SDA_CLR;
}
u8 CH450_I2C_ReceACK()
{
CH450_SDA_IN();
CH450_SCL_CLR;
DELAY;
CH450_SCL_SET;
DELAY;
if (CH450_RD_SDA)
{
CH450_SCL_CLR;
DELAY;
//CH450_PORT &= ~SDA;
return 1;
}
else
{
CH450_SCL_CLR;
DELAY;
//CH450_PORT &= ~SDA;
return 0;
}
}
/***********************************************
*Function Name :CH450_I2c_RdByte
*Description :I2C 讀一個字節
*Input :None
*Output :None
*Return :dat
************************************************/
u8 CH450_I2c_RdByte(void) //讀一個字節數據
{
u8 dat,i;
u16 j;
CH450_SDA_OUT(); //CPU釋放SDA引腳
DELAY;
CH450_SDA_SET;
DELAY;
CH450_SDA_IN(); /* 設置SDA為輸入方向 */
DELAY;
dat = 0;
for (i = 0; i != 8; i++) // 輸入8位數據
{
CH450_SCL_SET;
DELAY;
dat <<= 1;
j = CH450_RD_SDA;
if (j)
{
dat++;
} // 輸入1位
DELAY;
CH450_SCL_CLR;
DELAY;
}
CH450_SDA_OUT();
DELAY;
CH450_SDA_SET;
DELAY;
CH450_SCL_SET;
DELAY;
CH450_SCL_CLR;
DELAY;
return(dat);
}
/***********************************************
*Function Name :CH450_Write
*Description :CH450寫命令
*Input :command 寫命令
*Output :None
*Return :None
************************************************/
void CH450_Write(u16 command) //寫命令
{
CH450_I2c_Start(); /*啟動總線*/
CH450_I2c_WrByte((u8)(command >> 7) & CH450_I2C_MASK | CH450_I2C_ADDR0); // CH450的ADDR=0時
CH450_I2c_WrByte((u8)command); /*發送數據*/
CH450_I2c_Stop(); /*結束總線*/
}
/***********************************************
*Function Name :CH450_Read
*Description :CH450讀命令
*Input :None
*Output :None
*Return :val
************************************************/
u8 CH450_Read( void)
{
u8 val;
CH450_I2c_Start();/*啟動總線*/
DELAY;
// CH450_I2c_WrByte((u8)(CH450_GET_KEY>>7)&CH450_I2C_MASK|CH450_I2C_ADDR0|0x01); // 若有兩個CH450并連,當ADDR=0時,需修改為CH450_I2C_ADDR0
CH450_I2c_WrByte(0x4F);//讀取按鍵代碼輸出字節1
DELAY;
val = CH450_I2c_RdByte(); /*讀取數據*/
CH450_I2c_Stop(); /*結束總線*/
DELAY;
return val;
}
/***********************************************
*Function Name :CH450_Init
*Description :CH450的初始化函數
*Input :None
*Output :None
*Return :None
************************************************/
void CH450_Init( void )
{
/* Enable GPIOG's AHB interface clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
CH450_SCL_OUT();
DELAY;
CH450_Write(0x0403);//開顯示鍵盤
DELAY;
}
#ifndef _CH450_H_
#define _CH450_H_
#include "stm32f4xx.h"
/***********************************************
*Function Name :mDelayuS
*Description :
*Input :n 延時時間
*Output :None
*Return :None
************************************************/
void mDelayuS(u16 n);
/***********************************************
*Function Name :mDelaymS
*Description :
*Input :delay 延時時間
*Output :None
*Return :None
************************************************/
void mDelaymS(u16 delay);
/***********************************************
*Function Name :CH450_I2c_Start
*Description :I2C START
*Input :None
*Output :None
*Return :None
************************************************/
void CH450_I2c_Start( void );
/***********************************************
*Function Name :CH450_I2c_Stop
*Description :I2C STOP
*Input :None
*Output :None
*Return :None
************************************************/
void CH450_I2c_Stop( void );
/***********************************************
*Function Name :CH450_I2c_WrByte
*Description :I2C 寫一個字節
*Input :c 待寫字節數據
*Output :None
*Return :None
************************************************/
void CH450_I2c_WrByte(u8 c);
/***********************************************
*Function Name :CH450_I2c_RdByte
*Description :I2C 讀一個字節
*Input :None
*Output :None
*Return :dat
************************************************/
u8 CH450_I2c_RdByte( void );
/***********************************************
*Function Name :CH450_Write
*Description :CH450寫命令
*Input :command 寫命令
*Output :None
*Return :None
************************************************/
void CH450_Write(u16 command);
/***********************************************
*Function Name :CH450_Read
*Description :CH450讀命令
*Input :None
*Output :None
*Return :val
************************************************/
u8 CH450_Read( void);
/***********************************************
*Function Name :CH450_Init
*Description :CH450的初始化函數
*Input :None
*Output :None
*Return :None
************************************************/
void CH450_Init( void );
#endif
|