您好,欢迎访问本站!
商品分类

AVR入门不难—-用CVAVR

作者: 倪大德

一直用ICCAVR,最近试用CVAVR做项目,感觉一个字“爽”。
为此想介绍给想入门或刚入门的朋友,共同学习!
********************************************************************

    常常发现一些朋友为做一个电子钟而在网上找LCD,18B20,ds1302资料,
有时又会为找来的资料不能用而烦恼!
    想少些烦恼吗!用CVAVR—“傻瓜级软件”(自定义)

      CVAVR可以会你自动产生代码:

    以编写一LCD为例:
    请看用CVAVR编写十分简洁。(为说明 源码已作注解!)
//*******************************************************
#include <mega8.h>
#include <lcd.h>
#include <delay.h>
#asm
   .equ __lcd_port=0x12; PORTD     //LCD接口定义
#endasm
void main(void)   
{
   lcd_init(16);                  //LCD初始化
   lcd_clear();                   //清屏
   lcd_gotoxy(0,0);               //从第一项0开始显示
   lcd_putsf(“www.yleee.net/“);
   lcd_gotoxy(0,1);               //从第二项1开始显示
   lcd_putsf(“CodeVision AVR”);
   while(1){}
}  
//*********************************************************

AVR入门不难----用CVAVR

CVAVR应用图

AVR入门不难----用CVAVR

需要说明的是:

    1.以上源码是由CVAVR自动生成,我的工作只是略加整理而已!

    2.CVAVR有丰富的库函数,可以从网上找到到读一下。

    3.用CVAVR,同样能编写出漂亮的源代码!

源代码和仿真图

AVR入门不难----用CVAVR

LCD_test_16x02.rar(27.3 KB, 下载次数: 95)

附:CVAVR源码赏析


以下源码仅供参考!

1604源代码

/*************************************/
#include <mega8.h>
#include <lcd.h>
#include <delay.h>
#asm
   .equ __lcd_port=0x12; PORTD
#endasm
void main(void)   
{
   lcd_init(16);
   lcd_clear();
   lcd_gotoxy(0,0);
   lcd_putsf(“LCD 16×04”);
   lcd_gotoxy(0,1);
   lcd_putsf(“Hello World!”);
   lcd_gotoxy(0,2);
   lcd_putsf(“www.yleee.net/“);
   lcd_gotoxy(0,3);
   lcd_putsf(“CodeVision AVR”);
   while(1){}
}

AVR入门不难----用CVAVR


//PWM1

#include <mega8.h>
void main( void )
{

  PORTB = 0;
  DDRB = 0xff;

  TIMSK = 0;

  TCCR0 = (1<<WGM01)|(1<<WGM00)|(1<<COM01)|(0<<COM00)|(0<<CS02)|(1<<CS01)|(1<<CS00);
  TCNT0 = 0;
  OCR0 = 0;

  ADMUX = (0<<REFS1)|(1<<REFS0)|(1<<ADLAR)|(0<<MUX3)|(0<<MUX2)|(0<<MUX1)|(0<<MUX0);

  ADCSRA = (1<<ADEN)|(1<<ADSC)|(1<<ADATE)|(1<<ADIE)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);

  SFIOR = 0;
    #asm(“sei”);
  while(1);
}

interrupt [ADC_INT] void ADC_vect(void)
{
  OCR0 = ADCH;
}

//*****************************************************************
//ADC1
#include <mega8.h>

#define StartConvAdc() ADCSRA |= (1<<ADSC)  
#define KEY_NULL  0
#define KEY_S1    1
#define KEY_S2    2
#define KEY_S3    3
#define KEY_S4    4

volatile unsigned char KeyBuf = 0;
void main( void )
{
  unsigned char tmp;

  DDRC = 0xff;
  PORTC = 0xff;

  ADMUX = (0<<REFS1)|(1<<REFS0)|(1<<ADLAR)|(0<<MUX3)|(0<<MUX2)|(0<<MUX1)|(0<<MUX0);

  ADCSRA = (1<<ADEN)|(0<<ADSC)|(0<<ADATE)|(1<<ADIE)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);

  #asm(“sei”);
  StartConvAdc();

  while(1)
  {
    tmp = KeyBuf;
    if (tmp)
    {
      tmp–;
      PORTC = ~(1<<tmp);
    }
    else
      PORTC = 0xff;
  }
}

interrupt [ADC_INT] void ADC_vect(void)
{

  unsigned char AdcBuf = ADCH;

  if (AdcBuf > 240)
    KeyBuf = KEY_S4;
  else if (AdcBuf > 180)
    KeyBuf = KEY_S3;
  else if (AdcBuf > 120)
    KeyBuf = KEY_S2;
  else if (AdcBuf > 50)
    KeyBuf = KEY_S1;
  else
    KeyBuf = KEY_NULL;

  StartConvAdc();
}

//******************************************************************

//由CVAVR自动产生的USART代码

#include <mega8.h>
#include <delay.h>
#define RXB8 1
#define TXB8 0
#define UPE 2
#define OVR 3
#define FE 4
#define UDRE 5
#define RXC 7
#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<OVR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)
// USART Transmitter buffer
#define TX_BUFFER_SIZE 8
char tx_buffer[TX_BUFFER_SIZE];
#if TX_BUFFER_SIZE<256
unsigned char tx_wr_index,tx_rd_index,tx_counter;
#else
unsigned int tx_wr_index,tx_rd_index,tx_counter;
#endif
// USART Transmitter interrupt service routine
interrupt [USART_TXC] void usart_tx_isr(void)
{
if (tx_counter)
   {
   –tx_counter;
   UDR=tx_buffer[tx_rd_index];
   if (++tx_rd_index == TX_BUFFER_SIZE) tx_rd_index=0;
   };
}
#ifndef _DEBUG_TERMINAL_IO_
// Write a character to the USART Transmitter buffer
#define _ALTERNATE_PUTCHAR_
#pragma used+
void putchar(char c)
{
while (tx_counter == TX_BUFFER_SIZE);
#asm(“cli”)
if (tx_counter || ((UCSRA & DATA_REGISTER_EMPTY)==0))
   {
   tx_buffer[tx_wr_index]=c;
   if (++tx_wr_index == TX_BUFFER_SIZE) tx_wr_index=0;
   ++tx_counter;
   }
else
   UDR=c;
#asm(“sei”)
}
#pragma used-
#endif
// Standard Input/Output functions
#include <stdio.h>
// Declare your global variables here
void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
TCCR0=0x00;
TCNT0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
MCUCR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0x00;
UCSRB=0x58;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x33;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// Global enable interrupts
#asm(“sei”)
while (1)
      {
    unsigned char d;
    //  printf(“Hello!”);

   // while (1)
    //{
       d=getchar();
       putchar(d);

    //};   
    delay_ms(2000);
  };
}

更多内容详见: https://www.yleee.com.cn/thread-3061-1-1.html

发表回复

登录后才能评论