01.08 第五讲
动态扫描的编程
在数码管中,比较有特色的恐怕就是动态扫描了,现在我们进行显示“2009”
1、主要功能:显示“2009”
2、硬件接口:
PORTA------数码管的A-F
LED1--------PORTB.2
LED2--------PORTB.3
LED3--------PORTB.4
LED4--------PORTB.5
3、建立项目
4、编写程序,如下:
//ICC-AVR application builder : 2009-1-7 12:40:07
// Target : M16
// Crystal: 7.3728Mhz
//南京华岳电子
http://www.njhuayue.com
//淘宝店铺:
http://shop36473995.taobao.com/
#include <iom16v.h>
#include <macros.h>
//*********************************8
//设置参数
//***********************************
unsigned char LED1,LED2,LED3,LED4,CNT;
//**********************************
//IO初始化
//**********************************
void port_init(void)
{
PORTA = 0x3C;
DDRA = 0x3C;
PORTB = 0xFF;
DDRB = 0xFF;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
//**********************************
//定时器初始化
//**********************************
//TIMER0 initialize - prescale:1024
// WGM: Normal
// desired value: 1mSec
// actual value: 0.972mSec (2.8%)
void timer0_init(void)
{
TCCR0 = 0x00; //stop
TCNT0 = 0xF9; //set count
OCR0 = 0x07; //set compare
TCCR0 = 0x05; //start timer
}
//**********************************
//定时器中断
//**********************************
#pragma interrupt_handler timer0_ovf_isr:10
void timer0_ovf_isr(void)
{
TCNT0 = 0xF9; //reload counter value
switch(CNT)
{
case 0: //显示第一位数码管
{ PORTA|=0B00111100;
PORTA&=0B11111011;
PORTB=LED1;
CNT+=1;
break;
}
case 1: //显示第一位数码管
{
PORTA|=0B00111100;
PORTA&=0B11110111;
PORTB=LED2;
CNT+=1;
break;
}
case 2: //显示第一位数码管
{
PORTA|=0B00111100;
PORTA&=0B11101111;
PORTB=LED3;
CNT+=1;
break;
}
case 3: //显示第一位数码管
{
PORTA|=0B00111100;
PORTA&=0B11011111;
PORTB=LED4;
CNT=0;
break;
}
default: //CNT数据不对的时候能够返回
{
CNT=0;
break;
}
}
}
//*****************************
//初始化器件
//*****************************
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer0_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x01; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//*****************************
//主程序
//*****************************
void main(void)
{
init_devices();
//insert your functional code here...
CNT=0;
LED1=0B01011011;//初始值
LED2=0B00111111;
LED3=0B00111111;
LED4=0B01101111;
while(1)
{
;
}
}
使用仿真器将程序下载以后可以看出运行的效果,如下图所示:
第六讲 串口中断
现在我们在对通讯这一也是要求比较高的,那么,我们学习一下串口的程序,
1、主要功能:串口通讯
2、硬件接口:
串口和PC连接
3、建立项目
4、编写程序,如下:
#include <iom16v.h>
#include <macros.h>
//南京华岳电子
http://www.njhuayue.com
//淘宝店铺:
http://shop36473995.taobao.com/
//***************************
//设置变量参数
//***************************
unsigned char DATA1,DATA2;
//***************************
//初始化IO
//***************************
void port_init(void)
{
PORTA = 0x40;
DDRA = 0x40;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
//***************************
//设置串口参数
//***************************
//UART0 initialize
// desired baud rate: 9600
// actual: baud rate:9600 (0.0%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
UCSRB = 0x00; //disable while setting baud rate
UCSRA = 0x00;
UCSRC = BIT(URSEL) | 0x06;
UBRRL = 0x2F; //set baud rate lo
UBRRH = 0x00; //set baud rate hi
UCSRB = 0xD8;
}
//***************************
//串口中断接收程序
//***************************
#pragma interrupt_handler uart0_rx_isr:12
void uart0_rx_isr(void)
{
//uart has received a character in UDR
DATA2=DATA1;
DATA1=UDR;
}
//***************************
//串口中断程序
//***************************
#pragma interrupt_handler uart0_tx_isr:14
void uart0_tx_isr(void)
{
//character has been transmitted
}
//***************************
//串口发送程序
//***************************
void USART0_Transmit(unsigned char data)
{
while(!(UCSRA&(1<<UDRE)));
UDR=data;
}
//***************************
//初始化器件程序
//***************************
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
uart0_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//***************************
//主程序
//***************************
void main(void)
{
init_devices();
//insert your functional code here...
USART0_Transmit(0X33);
while(1)
{
if(DATA2==0X33)
{
PORTA=0X00;
}
}
}
第七讲 外部中断的使用
使用外部中断0,按键按下,中断0内部LED取反
1、主要功能:外部中断0
2、硬件接口:
按键和LED
3、建立项目
4、编写程序,如下:
#include <iom16v.h>
#include <macros.h>
//南京华岳电子
http://www.njhuayue.com
//淘宝店铺:
http://shop36473995.taobao.com/
#include <iom16v.h>
#include <macros.h>
//**********************************
//定义LED取反的程序
//**********************************
#define LED_COM PORTA ^= (1 << PA6) //位取反
//**********************************
//初始化IO
//**********************************
void port_init(void)
{
PORTA = 0x40;
DDRA = 0x40;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x04;
DDRD = 0x00;
}
//**********************************
//外部中断0的程序
//**********************************
#pragma interrupt_handler int0_isr:2
void int0_isr(void)
{
LED_COM;
}
//**********************************
//初始化芯片
//**********************************
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
MCUCR = 0x00;
GICR = 0x40;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//**********************************
//主程序
//**********************************
void main()
{
init_devices();
while(1)
{
;
}
}