[STM8] IAR STM8 C++ IO操作第二版

[复制链接]
8239|30
 楼主| CC2530 发表于 2011-7-2 15:34 | 显示全部楼层 |阅读模式
本帖最后由 CC2530 于 2011-7-2 16:23 编辑

#include "stm8l15x_conf.h"
#include "uni_int.h"
#include "iar_macro.h"
#include "util_macro.h"
#define STM8_IO_REG_WITH_DELAY   1
#if STM8_IO_REG_WITH_DELAY==1
void __delay_1(uint16 n);
#endif
template <uint8 C> class  STM8_IO_REG_BIT_t
{
  public:
   
    __always_inline__ static  STM8_IO_REG_BIT_t<C>  &Attach(volatile GPIO_TypeDef * REG_ADDR)
    {
        return *(STM8_IO_REG_BIT_t<C> *)REG_ADDR;
    }
   
    GPIO_TypeDef GPIO;
   
    __always_inline__ STM8_IO_REG_BIT_t<C> &Set()
    {
        GPIO.ODR |=  1U<<C;
        return *this;
    }
   
    __always_inline__ STM8_IO_REG_BIT_t<C> &Clr()
    {
        GPIO.ODR &= ~(1U<<C);
        return *this;
    }
   
   
    __always_inline__  STM8_IO_REG_BIT_t<C> &Toggle()
    {
        GPIO.ODR ^= 1U<<C;
        return *this;
    }
   
    __always_inline__  STM8_IO_REG_BIT_t<C> &Write(uint8 value)
    {
        if(value)
        {
            GPIO.ODR |=  1U<<C;
        }
        else
        {
            GPIO.ODR &= ~(1U<<C);
        }
        return *this;
    }
    __always_inline__  STM8_IO_REG_BIT_t<C> &Read(uint8 &value)
    {
        
        if(GPIO.IDR &  (1U<<C))
        {
            value=1;
        }
        else
        {
            value=0;
        }
        
        return *this;
    }
   
    __always_inline__  STM8_IO_REG_BIT_t<C> &MakeOut()
    {
        
        GPIO.DDR |=  _BV( C);
        return *this;
    }
   
    __always_inline__  STM8_IO_REG_BIT_t<C> &MakeIn()
    {      
        GPIO.DDR &= ~_BV( C);
        return *this;
    }
   
    __always_inline__  STM8_IO_REG_BIT_t<C> &SetCR1(void)
    {      
        GPIO.CR1 |=  _BV( C);
        return *this;
    }
   
    __always_inline__  STM8_IO_REG_BIT_t<C> &ClrCR1(void)
    {      
        GPIO.CR1 &= ~_BV( C);
        return *this;
    }
   
    __always_inline__  STM8_IO_REG_BIT_t<C> &SetCR2(void)
    {      
        GPIO.CR2 |=  _BV( C);
        return *this;
    }
   
    __always_inline__  STM8_IO_REG_BIT_t<C> &ClrCR2(void)
    {      
        GPIO.CR2 &= ~_BV( C);
        return *this;
    }
   
    __always_inline__  STM8_IO_REG_BIT_t<C> &MakeInputFloatingWithoutInterrupt(void)
    {
        this->MakeIn().ClrCR1().ClrCR2();
        return *this;
    }
   
    __always_inline__  STM8_IO_REG_BIT_t<C> &MakeInputPullupWithoutInterrupt(void)
    {
        this->MakeIn().SetCR1().ClrCR2();
        return *this;
    }
   
    __always_inline__  STM8_IO_REG_BIT_t<C> &MakeInputFloatingWithInterrupt(void)
    {
        this->MakeIn().ClrCR1().SetCR2();
        return *this;
    }
   
    __always_inline__  STM8_IO_REG_BIT_t<C> &MakeInputPullupWithInterrupt(void)
    {
        this->MakeIn().SetCR1().SetCR2();
        return *this;
    }
   
    __always_inline__  STM8_IO_REG_BIT_t<C> &MakeOutputOpenDrainSlow(void)
    {
        this->MakeOut().ClrCR1().ClrCR2();
        return *this;
    }
    __always_inline__  STM8_IO_REG_BIT_t<C> &MakeOutputPushPullSlow(void)
    {
        this->MakeOut().SetCR1().ClrCR2();
        return *this;
    }
    __always_inline__  STM8_IO_REG_BIT_t<C> &MakeOutputOpenDrainFast(void)
    {
        this->MakeOut().ClrCR1().SetCR2();
        return *this;
    }
    __always_inline__  STM8_IO_REG_BIT_t<C> &MakeOutputPushPullFast(void)
    {
        this->MakeOut().SetCR1().SetCR2();
        return *this;
    }
   
    __always_inline__  STM8_IO_REG_BIT_t<C> &ReadIn(uint8 &value)
    {
        
        if(GPIO.IDR &  (1U<<C))
        {
            value=1;
        }
        else
        {
            value=0;
        }
        
        return *this;
    }
   
    __always_inline__  STM8_IO_REG_BIT_t<C> &ReadOut(uint8 &value)
    {
        
        if(GPIO.ODR &  (1U<<C))
        {
            value=1;
        }
        else
        {
            value=0;
        }
        
        return *this;
    }
   
    __always_inline__  STM8_IO_REG_BIT_t<C> &ReadDir(uint8 &value)
    {
        
        if(GPIO.DDR &  (1U<<C))
        {
            value=1;
        }
        else
        {
            value=0;
        }
        
        return *this;
    }
   
#if STM8_IO_REG_WITH_DELAY==1
    __always_inline__  STM8_IO_REG_BIT_t<C> &Delay(uint16 n)
    {
        __delay_1(n);
        return *this;
    }
#endif   
   
};

template <uint8 C,uint8 P> class  STM8_IO_POLARITY_REG_BIT_t
{
  public:  
    __always_inline__ static  STM8_IO_POLARITY_REG_BIT_t<C,P>  &Attach(volatile GPIO_TypeDef * REG_ADDR)
    {
        return *(STM8_IO_POLARITY_REG_BIT_t<C,P> *)REG_ADDR;
    }
   
    GPIO_TypeDef GPIO;
   
    __always_inline__ STM8_IO_POLARITY_REG_BIT_t<C,P> &Set()
    {
        GPIO.ODR |=  1U<<C;
        return *this;
    }
   
    __always_inline__ STM8_IO_POLARITY_REG_BIT_t<C,P> &Clr()
    {
        GPIO.ODR &= ~(1U<<C);
        return *this;
    }
   
   
    __always_inline__  STM8_IO_POLARITY_REG_BIT_t<C,P> &Toggle()
    {
        GPIO.ODR ^= 1U<<C;
        return *this;
    }
   
    __always_inline__  STM8_IO_POLARITY_REG_BIT_t<C,P> &Write(uint8 value)
    {
        if(value)
        {
            GPIO.ODR |=  1U<<C;
        }
        else
        {
            GPIO.ODR &= ~(1U<<C);
        }
        return *this;
    }
    __always_inline__  STM8_IO_POLARITY_REG_BIT_t<C,P> &Read(uint8 &value)
    {
        
        if(GPIO.IDR &  (1U<<C))
        {
            value=1;
        }
        else
        {
            value=0;
        }
        
        return *this;
    }
   
    __always_inline__  STM8_IO_POLARITY_REG_BIT_t<C,P> &MakeOut()
    {
        
        GPIO.DDR |=  _BV( C);
        return *this;
    }
   
    __always_inline__  STM8_IO_POLARITY_REG_BIT_t<C,P> &MakeIn()
    {      
        GPIO.DDR &= ~_BV( C);
        return *this;
    }
   
    __always_inline__  STM8_IO_POLARITY_REG_BIT_t<C,P> &SetCR1(void)
    {      
        GPIO.CR1 |=  _BV( C);
        return *this;
    }
   
    __always_inline__  STM8_IO_POLARITY_REG_BIT_t<C,P> &ClrCR1(void)
    {      
        GPIO.CR1 &= ~_BV( C);
        return *this;
    }
   
    __always_inline__  STM8_IO_POLARITY_REG_BIT_t<C,P> &SetCR2(void)
    {      
        GPIO.CR2 |=  _BV( C);
        return *this;
    }
   
    __always_inline__  STM8_IO_POLARITY_REG_BIT_t<C,P> &ClrCR2(void)
    {      
        GPIO.CR2 &= ~_BV( C);
        return *this;
    }
   
    __always_inline__  STM8_IO_POLARITY_REG_BIT_t<C,P> &MakeInputFloatingWithoutInterrupt(void)
    {
        this->MakeIn().ClrCR1().ClrCR2();
        return *this;
    }
   
    __always_inline__  STM8_IO_POLARITY_REG_BIT_t<C,P> &MakeInputPullupWithoutInterrupt(void)
    {
        this->MakeIn().SetCR1().ClrCR2();
        return *this;
    }
   
    __always_inline__  STM8_IO_POLARITY_REG_BIT_t<C,P> &MakeInputFloatingWithInterrupt(void)
    {
        this->MakeIn().ClrCR1().SetCR2();
        return *this;
    }
   
    __always_inline__  STM8_IO_POLARITY_REG_BIT_t<C,P> &MakeInputPullupWithInterrupt(void)
    {
        this->MakeIn().SetCR1().SetCR2();
        return *this;
    }
   
    __always_inline__  STM8_IO_POLARITY_REG_BIT_t<C,P> &MakeOutputOpenDrainSlow(void)
    {
        this->MakeOut().ClrCR1().ClrCR2();
        return *this;
    }
    __always_inline__  STM8_IO_POLARITY_REG_BIT_t<C,P> &MakeOutputPushPullSlow(void)
    {
        this->MakeOut().SetCR1().ClrCR2();
        return *this;
    }
    __always_inline__  STM8_IO_POLARITY_REG_BIT_t<C,P> &MakeOutputOpenDrainFast(void)
    {
        this->MakeOut().ClrCR1().SetCR2();
        return *this;
    }
    __always_inline__  STM8_IO_POLARITY_REG_BIT_t<C,P> &MakeOutputPushPullFast(void)
    {
        this->MakeOut().SetCR1().SetCR2();
        return *this;
    }
   
    __always_inline__  STM8_IO_POLARITY_REG_BIT_t<C,P> &ReadIn(uint8 &value)
    {
        
        if(GPIO.IDR &  (1U<<C))
        {
            value=1;
        }
        else
        {
            value=0;
        }
        
        return *this;
    }
   
    __always_inline__  STM8_IO_POLARITY_REG_BIT_t<C,P> &ReadOut(uint8 &value)
    {
        
        if(GPIO.ODR &  (1U<<C))
        {
            value=1;
        }
        else
        {
            value=0;
        }
        
        return *this;
    }
   
    __always_inline__  STM8_IO_POLARITY_REG_BIT_t<C,P> &ReadDir(uint8 &value)
    {
        
        if(GPIO.DDR &  (1U<<C))
        {
            value=1;
        }
        else
        {
            value=0;
        }
        
        return *this;
    }
   
   
    __always_inline__ STM8_IO_POLARITY_REG_BIT_t<C,P>  &On()
    {
        if(P)
        {
            this->Set();
        }
        else
        {
            this->Clr();
        }
        return *this;
    }
   
    __always_inline__ STM8_IO_POLARITY_REG_BIT_t<C,P> &Off()
    {
        if(P)
        {
            this->Clr();
        }
        else
        {
            this->Set();
        }
        return *this;
    }
   
#if STM8_IO_REG_WITH_DELAY==1
    __always_inline__  STM8_IO_POLARITY_REG_BIT_t<C,P>  &Delay(uint16 n)
    {
        __delay_1(n);
        return *this;
    }
#endif
   
};
uint8 x;
#define LED_1     (STM8_IO_REG_BIT_t<1>::Attach(GPIOD))             //LED_1:PD:1
#define LED_2     (STM8_IO_POLARITY_REG_BIT_t<2,1>::Attach(GPIOD))  //LED_2:PD2,高电平亮
void test_io(void)
{   
    LED_1.MakeOutputPushPullFast().Set().Delay(100).Clr().Delay(100).Toggle().Delay(100).Write(0).Delay(100).Write(1).Delay(100).MakeInputFloatingWithoutInterrupt().Delay(100).Read(x).MakeOutputPushPullFast().Write(x).Delay(100);
   
    LED_2.MakeOutputPushPullFast().On().Delay(100).Off().Delay(100).Set().Delay(100).Clr().Delay(100).Toggle().Delay(100).Write(0).Delay(100).Write(1).Delay(100).MakeInputFloatingWithoutInterrupt().Delay(100).Read(x).MakeOutputPushPullFast().Write(x).Delay(100);   
}
int main()
{
    test_io();
   
    while(1);
}

stm8l15x_io_cpp.rar

1.13 MB, 下载次数: 145

乡村男孩 发表于 2011-7-2 15:37 | 显示全部楼层
乡村男孩 发表于 2011-7-2 15:38 | 显示全部楼层
上官总是能带来些好玩的东西
 楼主| CC2530 发表于 2011-7-2 15:39 | 显示全部楼层
再加Delay()应该更好玩。

LED_1.On().Delay(100).Off().Delay(100);
乡村男孩 发表于 2011-7-2 15:47 | 显示全部楼层
上官发个工程玩玩
乡村男孩 发表于 2011-7-2 15:48 | 显示全部楼层
周末学习C++
争取能看懂:P
乡村男孩 发表于 2011-7-2 16:10 | 显示全部楼层
纠结啊,
单片机仿真到一半就复位
XXXXXX00000000XXXXXXXXXXXXXX
hotpower 发表于 2011-7-2 17:01 | 显示全部楼层
哈哈,和酒鬼与Lee老师一样的暴力
yangaiping 发表于 2011-7-2 17:01 | 显示全部楼层
努力学习c++!
乡村男孩 发表于 2011-7-2 17:03 | 显示全部楼层
Cpp学习热潮
hotpower 发表于 2011-7-2 17:06 | 显示全部楼层
Lee老师的LOOK早已在M3上实现。
等ST出M0后再继续~~~

菜农助学园地招募三名LOOK志愿者
乡村男孩 发表于 2011-7-2 17:07 | 显示全部楼层
LEE老师太猛了
我菜鸟
teet 发表于 2011-7-3 11:23 | 显示全部楼层
Cpp:victory:
teet 发表于 2011-7-3 11:23 | 显示全部楼层
都是牛人呀
daifuquan 发表于 2011-7-3 18:18 | 显示全部楼层
1# CC2530 C++?
fydx 发表于 2011-7-3 23:35 | 显示全部楼层
Ryanhsiung 发表于 2011-7-4 08:53 | 显示全部楼层
有些语法还是看不懂!.delay(1).delay(1);  这样也行? 我记得C++好像没有这样的指令。
wujun23941105 发表于 2011-7-16 21:03 | 显示全部楼层
秋天落叶 发表于 2011-7-17 11:30 | 显示全部楼层
要完全看明白也不容易啊
hsbjb 发表于 2011-7-17 20:26 | 显示全部楼层
说明好像有点少了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:该死的ST,还不赶快出STM8L小FLASH片子

0

主题

262

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部