stm8s用硬件iic的时候,stvp需要做哪些设置?我设置了op
tion byte的AFR6,除此之外还有哪些要改的?系统时钟应该是默认的HSI/8,2Mhz。
代码如下;
#include "stm8s.h"
#include "stm8s_conf.h"
#define SN3731_IIC_ADDRESS_M 0xe8
//包括8桢的图片寄存器地址段选择和一个控制寄存器地址段的选择
#define Reg_MainAddress 0xFD//寄存器主地址
//寄存器地址段选择(定义数据区域为1-8桢的数据)
#define Reg_Section_Fram(i) ((i)-1)//i=1-8
#define Reg_Section_CTRLReg 0x0B
//一幅SN3731开关寄存器定义
//i=1-9
#define Reg_ON_OFF_CS 0x00
#define Reg_ON_OFF_CSA(i) (Matrix_ON_OFF_CS+2*(i)-2) //A屏亮灭行变量
#define Reg_ON_OFF_CSB(i) (Matrix_ON_OFF_CS+2*(i)-1) //B屏亮灭行变量
//一幅SN3731闪烁寄存器定义
//i=1-9
#define Reg_Blink_CS 0x12
#define Reg_Blink_CSA(i) (Matrix_Blink_CS+2*(i)-2) //A屏闪烁行变量
#define Reg_Blink_CSB(i) (Matrix_Blink_CS+2*(i)-1) //B屏闪烁行变量
//一幅SN3731每颗灯灰度寄存器定义
//1-144
#define Reg_PWM(i) (0x25+(i)-2) //PWM地址偏移变量
//控制寄存器地址定义
#define Reg_ModeConfig 0x00 //模式配置寄存器
#define Reg_PictureAddress 0x01 //图片选择寄存器
#define Reg_FramePlay 0x02 //画面自动播放寄存器
#define Reg_FrameTime 0x03 //画面延时寄存器
#define Reg_0x04H 0x04 //NC
#define Reg_BlinkCtrl 0x05 //闪烁模式寄存器
#define Reg_AudioSync 0x06 //音乐同步寄存器
#define Reg_StatusReg 0x07 //画面状态寄存器(只读)
#define Reg_BreathTime 0x08 //呼吸时间设置寄存器
#define Reg_Breath 0x09 //呼吸控制寄存器
#define Reg_ShutDown 0x0A //关断寄存器
#define Reg_AGC 0x0B //AGC控制寄存器
#define Reg_AudioADCRate 0x0C //音频采样寄存器
//
#define Config_Mode_Picture 0x00
#define Config_Mode_Movie (0x01<3)
#define Config_Mode_AudioSync (0x02<3)
#define Software_Shutdown_Mode 0x00
#define Normal_Operation_Mode 0x01
//Start frame in Movie Mode and Picture Mode
//第 i frame
#define Config_Strat_Frame(i) (Config_Strat_Frame+(i)-1)
//Num of Loop in Moive mode
//第 i frame
#define Config_LoopNum_Movie(i) ((Config_LoopNum_Movie+(i))<3)
//Num of frame in Moive mode
//第 i frame
#define Config_FrameNum_Movie(i) (Config_FrameNum_Movie+(i))
//Delay between frame change in a movie
//i=0-63,当i=0,时间最大,其他情况下时间为i*30ms,最大为30ms*63=1.89s
#define Config_Delay_BetweenFrame_Movie(i) (i)//2.394s
u8 all_led_on[18]=
{
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
// 0x00,0x00,0xff,0xff,0x00,0x00,0xff,0xff,0x00,0x00,0xff,0xff,0x00,0x00,0xff,0xff,0x00,0x00
};
void IIC_BufferWrite(u8 Address,u8 *DataBuff,u16 DataLen)
{
while(I2C_GetFlagStatus(I2C_FLAG_BUSBUSY));//等待空闲才能操作
// ___
//发送起始信号 |/__
I2C_GenerateSTART( ENABLE );
while(!I2C_CheckEvent( I2C_EVENT_MASTER_START_SENT));
//发送从机地址器件,I2C主设备为写模式
I2C_Send7bitAddress(SN3731_IIC_ADDRESS_M, I2C_DIRECTION_TX);
//检测从器件返回一个应答信号
while(!I2C_CheckEvent( I2C_EVENT_MASTER_ADDRESS_ACKED ));
//发送字地址
I2C_SendData( Address );
while(!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTING));
while(DataLen--)
{
I2C_SendData(*DataBuff++);
while (!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_AcknowledgeConfig(I2C_ACK_CURR);
}
// __
//发送停止信号 __/| |
I2C_GenerateSTOP( ENABLE );
}
void SN3731_SelectSection(u8 MainAddress,u8 SectionReg)
{
while(I2C_GetFlagStatus(I2C_FLAG_BUSBUSY));//等待空闲才能操作
// ___
//发送起始信号 |/__
I2C_GenerateSTART( ENABLE );
while(!I2C_CheckEvent( I2C_EVENT_MASTER_START_SENT));
//发送从机地址器件,I2C主设备为写模式
I2C_Send7bitAddress(SN3731_IIC_ADDRESS_M, I2C_DIRECTION_TX);
//检测从器件返回一个应答信号
while(!I2C_CheckEvent( I2C_EVENT_MASTER_ADDRESS_ACKED ));
//发送字地址
I2C_SendData( MainAddress );
while(!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTING));
I2C_SendData( SectionReg );
while(!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTING));
// __
//发送停止信号 __/| |
I2C_GenerateSTOP( ENABLE );
}
void SN3731_Write_Reg(u8 reg,u8 data)
{
while(I2C_GetFlagStatus(I2C_FLAG_BUSBUSY));//等待空闲才能操作
// ___
//发送起始信号 |/__
I2C_GenerateSTART( ENABLE );
while(!I2C_CheckEvent( I2C_EVENT_MASTER_START_SENT));
//发送从机地址器件,I2C主设备为写模式
I2C_Send7bitAddress(SN3731_IIC_ADDRESS_M, I2C_DIRECTION_TX);
//检测从器件返回一个应答信号
while(!I2C_CheckEvent( I2C_EVENT_MASTER_ADDRESS_ACKED ));
//发送字地址
I2C_SendData( reg );
while(!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTING));
I2C_SendData( data );
while(!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTING));
// __
//发送停止信号 __/| |
I2C_GenerateSTOP( ENABLE );
}
int main( void )
{
I2C_DeInit();
I2C_Init(100000,
0x00,
I2C_DUTYCYCLE_2,
I2C_ACK_CURR,
I2C_ADDMODE_7BIT,
2);
while(1)
{
SN3731_SelectSection(Reg_MainAddress,Reg_Section_Fram(1));
IIC_BufferWrite(Reg_ON_OFF_CS,all_led_on,18);
SN3731_SelectSection(Reg_MainAddress,Reg_Section_CTRLReg);
SN3731_Write_Reg(Reg_ModeConfig,0x00);
SN3731_Write_Reg(Reg_PictureAddress,0x00);
}
}
一周热门 更多>