2015-7-15 17:02| 发布者: waveshare-admin| 查看: 8127| 评论: 0
本设计基于内部RC振荡器2M时钟作STM8定时器TIM4的时钟源进行I2C通讯LED控制电路原理图PCF8974原理图
/* -------------------------------------------------------------------实验内容:使用STM8的I2C与PCF8574T通讯,并使用LED作出简单指示。----------------------------------------------------------------------硬件连接:将PB口的LED指示灯使能开关切换到"ON"状态。----------------------------------------------------------------------*/
#include <STM8S207RB.h>#include <ws_pcf8574.h>unsigned char i=0;
#define I2C_busy() while((I2C_SR3&0x02)==1);#define I2C_start() I2C_CR2|=0x01;#define I2C_stop() I2C_CR2|=0x02;#define I2C_Ack() I2C_CR2|=0x04;#define I2C_NAck() I2C_CR2&=(~0x04);#define I2C_addrask (I2C_SR1&0x02)#define I2C_SBask (I2C_SR1&0x01)#define I2C_TxEask (I2C_SR1&0x80)#define I2C_RxNeask (I2C_SR1&0x40)#define I2C_BTF (I2C_SR1&0x04)#define I2C_BERR_ERR (I2C_SR2&0x01)#define I2C_AF_ERR (I2C_SR2&0x04)#define I2C_writebyte(x) I2C_DR=x;
void I2C_Init(void){ I2C_FREQR=0x0f; I2C_CCRH=0x00; I2C_CCRL=0x50; I2C_TRISER=0x11; I2C_CR1|=0x01; I2C_Ack(); I2C_CR2|=0x08;}void I2C_Start(void){ I2C_busy(); I2C_start(); while(I2C_SBask==0);}void I2C_SendDAdr(u8 Add){ I2C_writebyte(Add); while(I2C_addrask==0); I2C_busy();}void I2C_SendDat(u8 data){ I2C_writebyte(data); I2C_busy(); while(I2C_TxEask==0);}u8 I2C_RcvDat(void){ u8 i; while(I2C_RxNeask==0); abc: if(I2C_BTF==0x04) {i=I2C_DR;} else goto abc; if(I2C_BTF==0){} else goto abc; return i;}void PCF8574_Init(void){ I2C_Init();}void PCF8574_WriteAByte(u8 data){ I2C_Start(); I2C_SendDAdr(0x40); I2C_SendDat(data); I2C_stop();}void main(void){ u8 i=0x4c; PCF8574_Init(); while(1){PCF8574_WriteAByte(i);;i++;delay_ms(100);}}
LED不断的闪烁,改变延迟的大小,LED闪烁的速度加快!
以上资料由微雪电子整理并发布,未经许可不得转载,否则追究相应责任!
评论
微雪官网|产品资料|手机版|小黑屋|微雪课堂. ( 粤ICP备05067009号 )
GMT+8, 2025-1-3 11:15 , Processed in 0.020137 second(s), 20 queries .