上面展示的,包括在片子自带的eeprom 0字节处读取/写入一个16位的整数。只需要用eeuint(0)就可以代表这个数,不需要复杂的指针操作等等。
每次上电,debug输出的数字就会加上1或者2,代表上电次数。
timer4是一个8位定时器,用标准固件库提供的初始化函数和设置函数很麻烦,这里直接用timer4(1ms)就能够让timer4开始计时,且每个毫秒中断一次。再用timer4()就可以知道timer4总共中断的次数,这样也就知道程序从开始到现在运行了多长时间。用一个模运算实现的状态机控制led的亮灭,即是每500毫秒,有250毫秒点亮led,250毫秒熄灭。
附上目前进度:
//stmduino.h
#include <stm8s.h>
#include <stm8s_tim1.h>
#include <stm8s_tim4.h>
#include <stm8s_clk.h>
#include <stm8s_gpio.h>
#include <stm8s_itc.h>
#include <stm8s_i2c.h>
#include <stm8s_exti.h>
//#include <stm8s_uart2.h>
#include <stm8s_uart1.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#ifndef _STMDUINO_H__
#define _STMDUINO_H__
#define interrupts(state) INTERRUPTSTATE_##state
#define INTERRUPTSTATE_ enableInterrupts()
#define INTERRUPTSTATE_OFF disableInterrupts()
#define INTERRUPTSTATE_ON INTERRUPTSTATE_
#define PINBIT(x) ((GPIO_Pin_TypeDef)(1<<x))
#define HIGH (!LOW)
#define LOW 0
#define PORTOF_A1 GPIOA
#define PINOF_A1 PINBIT(1)
#define PORTOF_A2 GPIOA
#define PINOF_A2 PINBIT(2)
#define PORTOF_A3 GPIOA
#define PINOF_A3 PINBIT(3)
#define PORTOF_B4 GPIOB
#define PINOF_B4 PINBIT(4)
#define PORTOF_B5 GPIOB
#define PINOF_B5 PINBIT(5)
#define PORTOF_C3 GPIOC
#define PINOF_C3 PINBIT(3)
#define PORTOF_C4 GPIOC
#define PINOF_C4 PINBIT(4)
#define PORTOF_C5 GPIOC
#define PINOF_C5 PINBIT(5)
#define PORTOF_C6 GPIOC
#define PINOF_C6 PINBIT(6)
#define PORTOF_C7 GPIOC
#define PINOF_C7 PINBIT(7)
#define PORTOF_D3 GPIOD
#define PINOF_D3 PINBIT(3)
#define PORTOF_D4 GPIOD
#define PINOF_D4 PINBIT(4)
#define PORTOF_D5 GPIOD
#define PINOF_D5 PINBIT(5)
#define PORTOF_D6 GPIOD
#define PINOF_D6 PINBIT(6)
#define PORTOF_D1 GPIOD
#define PINOF_D1 PINBIT(1)
#define PORTOF_D2 GPIOD
#define PINOF_D2 PINBIT(2)
#define MODEOF_OUTPUTSHARP GPIO_MODE_OUT_PP_LOW_FAST
#define MODEOF_OUTPUT GPIO_MODE_OUT_PP_LOW_SLOW
#define MODEOF_INPUT GPIO_MODE_IN_PU_NO_IT
#define MODEOF_INPUTFL GPIO_MODE_IN_PU_NO_IT
#define pinMode(pin_to_set,mode_to_set) GPIO_Init(PORTOF_##pin_to_set,PINOF_##pin_to_set,MODEOF_##mode_to_set)
#define digitalWrite(pin_to_write,value_to_write) ((value_to_write==0)?GPIO_WriteLow(PORTOF_##pin_to_write,PINOF_##pin_to_write):GPIO_WriteHigh(PORTOF_##pin_to_write,PINOF_##pin_to_write))
#define digitalRead(pin_to_read) GPIO_ReadInputPin(PORTOF_##pin_to_read,PINOF_##pin_to_read))
#define pinHigh(pin_to_set) GPIO_WriteHigh(PORTOF_##pin_to_write,PINOF_##pin_to_write)
#define pinLow(pin_to_set) GPIO_WriteLow(PORTOF_##pin_to_write,PINOF_##pin_to_write)
#define pinFlip(pin_to_set) GPIO_WriteReverse(PORTOF_##pin_to_write,PINOF_##pin_to_write)
#define clock(cmd) CLOCK_##cmd
#define CLOCK_16M CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1)
#define CLOCK_8M CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV2)
#define CLOCK_4M CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV4)
#define CLOCK_2M CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV8)
#define CLOCK_1M {CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV8);CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV2);}
#define cpudivider(divider) CPUDIVIDER_##divider
#define CPUDIVIDER_1 CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1)
#define CPUDIVIDER_2 CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV2)
#define CPUDIVIDER_4 CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV4) //pwr saving if clock cpu down.
#define CPUDIVIDER_8 CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV8)
#define CPUDIVIDER_16 CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV16)
#define CPUDIVIDER_32 CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV32)
#define CPUDIVIDER_64 CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV64)
#define CPUDIVIDER_128 CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV128)
#define flash(cmd) FLASH_##cmd
#define FLASH_EEPROM FLASH_Unlock(FLASH_MEMTYPE_DATA)
#define eeprom(offset) (offset+0x4000)
#define eewriteuint(value,offset) (eeint(offset)=value)
#define eereaduint(offset) eeint(offset)
#define eeuint(offset) (*(unsigned int*)(eeprom(offset)))
unsigned long timer4_ticks=0;
INTERRUPT_HANDLER(TIM4_UPD_OVF_IRQHandler, 23)
{
//should timer4 tick every 1ms.
timer4_ticks+=1;
TIM4_ClearITPendingBit(TIM4_IT_UPDATE);//always clear pending bit!!!
}
#define TIMER4_SET_1ms timer4clock(64,250)
#define TIMER4_SET_100us timer4clock(16,100)
#define TIMER4_STATE_COUNT TIM4_Cmd(ENABLE)
#define TIMER4_STATE_STOP TIM4_Cmd(DISABLE)
#define TIMER4_STATE_TICK TIM4_ITConfig(TIM4_IT_UPDATE,ENABLE)
#define TIMER4_STATE_NOTICK TIM4_ITConfig(TIM4_IT_UPDATE,DISABLE)
#define TIMER4_STATE_ (timer4_ticks) //32bit counter
#define timer4tick(interval) TIMER4_SET_##interval
#define timer4clock(prescaler,counter) {TIM4_TimeBaseInit(TIM4_PRESCALER_##prescaler,counter);timer4(COUNT);timer4(TICK);}
#define timer4(state) TIMER4_STATE_##state
void assert_failed(u8* file, u32 line){while(1){}}
#endif