Blog

Blog

PHODAL

AVR-ADA在AVR单片机的运用

关于Ada:Ada语言最早是针对嵌入式和实时系统设计的,并且在今天依然在这方面使用广泛。Ada95版,是由INTERMETRICS公司的塔克·塔夫特于92到95年间设计的,当时主要是希望改进对于 系统,数字,财务软件编程的支持。

Ada语言的重要特征就是其嵌入式风格,模块化设计,编译检查,平行处理,异常处理及泛型编程。Ada在95年加入了对面向对象设计的支持,包括动态分配等。

Ada的编译检查主要是针对没有分配的内存读写的保护,堆栈溢出错误,单个错误空闲,队列读写错误以及其他可以避免的小问题。这些检查可以在为增加效率的情况下被取消,但是在编译的时候他们却能带来很高的效率。同样它也包括对程序的严正的设置。因为这些原因,它被广泛应用于一些非常重要的系统中,例如航空电子学,武器及航天飞行器的操作系统中。

同样它支持很多的编译时间检查,这些检查被用来避免一些错误的发生。这种错误往往是在其他语言中在运行以前都不能被察觉到的,需要在源码中加入特殊的检查设置才能被发现。

Ada的动态内存管理非常安全和高规格,它类似于JAVA语言却不同于C语言的。这种特殊功能并不需要特殊的运行设置。尽管这种语言的语意结构允许对于不能读写的目标进行自动的碎片搜集,但是大多数运行都不支持它。Ada却支持有限形式基于区域的存储管理。无效的读写常在运行时候被检查出来(除非这种检测被人为关闭)并且有时候在编译时候就被发现。

Ada语言的定义同国际标准化组织(ISO)的标准有很大不同,因为他是一个自由内容形式的。这种做法的后果是被广大程序员只能从它的标准化文档(普遍认为是Ada的参考使用手册(ARM))寻找细节性的技术问题,但是普遍情况是一本标准教科书却可以在其他不同语言上使用。

Ada语言由严格的巴斯特范式定义,但是不适合一般人阅读.它是第一种同时拥有IEC/ISO/美国军用标准认证的语言.其编译器经过严格的审查,以确保同样的代码在任一编译器上产生同样的可执行效果.并且保证并行性在代码级可以在无操作系统下同样运行。

关于AVR:

Atmel AVR系列是一种基于哈佛结构精简指令集(Reduced Instruction Set Computing, RISC)的微控制器,由Atmel公司于1996年研发。AVR系列是首次采用闪存(Flash Memory)作为数据存储介质的单芯片微控制器之一。

 

关于我的板子:

机电实验室的51开发板+AVR最小系统(Atmega16)。

 

关于开发环境:

WIN7+Emacs+Cygwin+Mingw+Win-AVR+AVR-ADA+USBAsp

 

我的Emacs的配置,

1、cd 到你的文件夹(比如,C:\Users\gmszone)

2、如果安装有git的话:

    git clone https://github.com/gmszone/Phodal-emacs.git

  其它的:

    打开迅雷下载:https://nodeload.github.com/gmszone/Phodal-emacs/zipball/master

3、复制出.emacs到个人文件夹

 

Win-AVR下载地址:

http://sourceforge.net/projects/winavr/files/

 

AVR-ADA下载地址:

http://sourceforge.net/projects/avr-ada/files/


OK:


流水灯,,,,核心代码如下,

with Interfaces;                   use Interfaces;
with AVR;                          use AVR;
with AVR.MCU;
with AVR.Interrupts;
with AVR.Timer0;
with AVR.Wait;

package body Walking_LED is

   --
   --  constant definitions
   --
   XTAL        : constant := 12_000_000;  -- Crystal frequency in Hz
   Timer_Clock : constant :=         2;  -- LED flashing frequency in 1Hz

   Count_Cmp   : constant := XTAL / 1024 / 256 / Timer_Clock - 1;


   --
   -- use volatile if variable is accessed from interrupts and in the
   -- main program.  No volatile if accessed in interrupt only.
   LED : Unsigned_8;

   Count : Unsigned_8;


   procedure Sig_Compare;
   pragma Machine_Attribute (Entity         => SIG_Compare,
                             Attribute_Name => "signal");
   pragma Export (C, Sig_Compare, Timer0.Signal_Compare);

   procedure Wait_1ms is new
     AVR.Wait.Generic_Wait_Usecs(Crystal_Hertz=>8_000_000,
                                 Micro_Seconds=>1000);
   
   procedure Wait_Long is 
   begin
      --delay 0.8;
      for J in 1..80 loop
           Wait_1ms;
      end loop;
   end Wait_Long;
   
   procedure Sig_Compare is
   begin
      Count := Count + 1;

      if Count > Count_Cmp then
         -- invert the output since a zero means: LED on
         MCU.PORTA := not LED;

         -- move to next LED
         LED := Rotate_Left (LED, 1);

         Count := 0;
      end if;
   end Sig_Compare;


   procedure Main is
   begin
      -- use all pins on PortB for output
      MCU.DDRA_Bits := (others => DD_Output);
      -- and turn off all LEDs
      MCU.PORTA := 16#FE#;
      
      for I in 1..8 loop 
         Wait_Long;
         MCU.PORTA:=Rotate_Left(MCU.PORTA,1);
      end loop;
      
      -- use CLK/1024 prescale value, clear timer/counter on compareA match
      Timer0.Init_CTC (Prescaler => Timer0.Scale_By_1024);

      -- enable Output Compare 1 overflow interrupt
      Timer0.Enable_Interrupt_Compare;

      -- init variable representing the LED state
      LED := 1;

      -- generally enable interrupts
      AVR.Interrupts.Enable;

      -- loop forever
      loop null; end loop;
   end Main;

end Walking_Led;



烧录到AVR单片机上:
1、敲入make,编译
2、敲入make program,烧录
      如果用的不是USBAsp只需,修改Makefile


或许您还需要下面的文章:

关于我

Github: @phodal     微博:@phodal     知乎:@phodal    

微信公众号(Phodal)

围观我的Github Idea墙, 也许,你会遇到心仪的项目

QQ技术交流群: 321689806
comment

Feeds

RSS / Atom

最近文章

关于作者

Phodal Huang

Engineer, Consultant, Writer, Designer

ThoughtWorks 技术专家

工程师 / 咨询师 / 作家 / 设计学徒

开源深度爱好者

出版有《前端架构:从入门到微前端》、《自己动手设计物联网》、《全栈应用开发:精益实践》

联系我: h@phodal.com

微信公众号: 最新技术分享

标签