|
乘着过年前最后一波打样,做一个工作灯
然后尝试用天问写了个比较完整的代码
- #define IB1 PA6
- #define IB2 PA1
- #define IG1 PA14
- #define IG2 PA4
- #define INC PB2
- #define DEC PB0
- #define SET PB6
- #define IR1 PA13
- #define IR2 PA7
- #define IR_I PB1
- #define LOPW PB3
- #define VBAT PA0
- #include <TWEN32F0xx.h>
- #include "HardwareSerial.h"
- #include "myLib/TW32f0xx_IRSendRev.h"
- #include "myLib/TW32f0xx_timer.h"
- #include "myLib/TW32f0xx_exti.h"
- void GPIO_init();
- uint8_t mod = 0;
- uint8_t val = 0;
- IRSendRev irsendrev;
- void mod0();
- void mod1();
- void mod2();
- void mod3();
- /*GPIO统一初始化
- */
- void GPIO_init(){
- pinMode(LOPW, GPIO_Mode_Out_PP);
- pinMode(IR1, GPIO_Mode_Out_PP);
- pinMode(IR2, GPIO_Mode_Out_PP);
- pinMode(IG1, GPIO_Mode_Out_PP);
- pinMode(IG2, GPIO_Mode_Out_PP);
- pinMode(IB1, GPIO_Mode_Out_PP);
- pinMode(IB2, GPIO_Mode_Out_PP);
- }
- void TIM_attachInterrupt_1() {
- //检测剩余电量
- uint8_t v = ADC_Read(ADC_PA0);
- Serial1.print("剩余电量: ");
- Serial1.println(v);
- //4096/5*2.8=2293.760
- if(v < 2295){
- digitalWrite(PB3, 1);
- mod = 0;
- }
- else{
- digitalWrite(PB3, 0);
- }
- }
- /*灯光关闭
- */
- void mod0(){
- digitalWrite(IR1, 0);
- digitalWrite(IR2, 0);
- digitalWrite(IG1, 0);
- digitalWrite(IG2, 0);
- digitalWrite(IB1, 0);
- digitalWrite(IB2, 0);
- }
- /*灯效1
- */
- void mod1(){
- }
- /*灯效2
- */
- void mod2(){
- }
- /*灯效3
- */
- void mod3(){
- }
- void TIM_attachInterrupt_3() {
- //切换灯光模式
- switch (mod) {
- case 0:
- mod0();
- break;
- case 1:
- mod1();
- break;
- case 2:
- mod2();
- break;
- case 3:
- mod3();
- break;
- }
- }
- void pin_attachInterrupt_fun_SET() {
- //SET按键 3种模式选择
- if(mod > 3){
- mod = mod + 1;
- val = 5;
- }
- else{
- mod = 0;
- }
- delay(500);
- }
- void pin_attachInterrupt_fun_DEC() {
- //DEC按键
- if(val > 0){
- val = val - 1;
- }
- delay(200);
- }
- void pin_attachInterrupt_fun_INC() {
- //INC按键 10档调节
- if(val < 10){
- val = val + 1;
- }
- delay(200);
- }
- int main(void)
- {
- TW32F0xx_init();
- TIM_attachInterrupt(TIM3, 1000000, TIM_attachInterrupt_3);
- TIM_attachInterrupt(TIM1, 10000000, TIM_attachInterrupt_1);
- Serial1.begin(115200);
- GPIO_init();
- irsendrev.begin(PA0);
- ADC_Init(ADC_PB1, ADC_SAMPLETIME_71CYCLES_5, ADC_RESOLUTION_12B);//ADC初始化
- TIM_Enable(TIM1);
- TIM_Enable(TIM3);
- Pin_attachInterrupt(SET,EXTI_Trigger_Rising,pin_attachInterrupt_fun_SET);
- Pin_attachInterrupt(DEC,EXTI_Trigger_Rising,pin_attachInterrupt_fun_DEC);
- Pin_attachInterrupt(INC,EXTI_Trigger_Rising,pin_attachInterrupt_fun_INC);
- while(1){
- if(irsendrev.available()){
- Serial1.print("红外接收地址: ");
- Serial1.println((irsendrev.recv_addr()));
- Serial1.print("红外接收数据: ");
- Serial1.println((irsendrev.recv()));
- }
- }
- return 1;
- }
复制代码
确实用着还行,对图形功能熟悉的话,基本上都能拼凑在一起 |
评分
-
1
查看全部评分
-
|