查看: 1477|回复: 0

【ATMEGA328P-XMINI】GPIO之按键led走起

[复制链接]
  • TA的每日心情
    开心
    2017-7-4 13:51
  • 签到天数: 347 天

    连续签到: 1 天

    [LV.8]以坛为家I

    发表于 2015-1-26 09:13:00 | 显示全部楼层 |阅读模式
    分享到:
    本周开始研究这块板子,相较于之前推出的其他板子而言,这块板子比较难入手的地方在于没有对应例程。尽管AtmeStudio能识别出这块板子,但并没有对应的例程可以调用。一时间让人很难下手。
    AVR这是我第一次用,作为初次使用者,首先得考虑搭建的平台。AtmeStudio是一个选择,另外也可以使用arduino IDE进行开发,这个我也没用过,也会去试试
    参考了下shaoziyang的例程
    /** GccTest.c** Created: 2015/1/7 10:50:35*  Author: uncle*/ #define F_CPU 16000000L#define LED PB5#include <avr/io.h>#include <util/delay.h>int main(void){    DDRB |= (1<<LED);    while(1)    {        PORTB ^= (1<<LED);        _delay_ms(100);    }}
    建一个空的工程文件

    在主函数中移入示例程序

    选型中选择mEDBG,后面的选择ISP,这是有点不同的

    编译,烧录程序

    led闪烁
    再回过头看看程序。
    发现尽管用的空工程,其实还是用了ASF的库
    #define DDRB _SFR_IO8(0x04)#define DDB0 0#define DDB1 1#define DDB2 2#define DDB3 3#define DDB4 4#define DDB5 5#define DDB6 6#define DDB7 7#define PORTB _SFR_IO8(0x05)#define PORTB0 0#define PORTB1 1#define PORTB2 2#define PORTB3 3#define PORTB4 4#define PORTB5 5#define PORTB6 6#define PORTB7 7void_delay_ms(double __ms){        double __tmp ; #if __HAS_DELAY_CYCLES && defined(__OPTIMIZE__) && \  !defined(__DELAY_BACKWARD_COMPATIBLE__) &&           \  __STDC_HOSTED__        uint32_t __ticks_dc;        extern void __builtin_avr_delay_cycles(unsigned long);        __tmp = ((F_CPU) / 1e3) * __ms;        #if defined(__DELAY_ROUND_DOWN__)                __ticks_dc = (uint32_t)fabs(__tmp);        #elif defined(__DELAY_ROUND_CLOSEST__)                __ticks_dc = (uint32_t)(fabs(__tmp)+0.5);        #else                //round up by default                __ticks_dc = (uint32_t)(ceil(fabs(__tmp)));        #endif        __builtin_avr_delay_cycles(__ticks_dc);#else        uint16_t __ticks;        __tmp = ((F_CPU) / 4e3) * __ms;        if (__tmp < 1.0)                __ticks = 1;        else if (__tmp > 65535)        {                //        __ticks = requested delay in 1/10 ms                __ticks = (uint16_t) (__ms * 10.0);                while(__ticks)                {                        // wait 1/10 ms                        _delay_loop_2(((F_CPU) / 4e3) / 10);                        __ticks --;                }                return;        }        else                __ticks = (uint16_t)__tmp;        _delay_loop_2(__ticks);#endif}理由便是我们调用的头文件#include <avr/io.h>#include <util/delay.h>恩,由此可见其实asf库也可以像st的那样方便的,毕竟该有的函数都有,前提是能找到。
    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条

    手机版|小黑屋|与非网

    GMT+8, 2024-4-29 13:32 , Processed in 0.120371 second(s), 18 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.