打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
基于ATmega8的PWM驱动程序

#include <mega8.h>
#include <delay.h>

#define uchar unsigned char
#define uint  unsigned int

 

#define zlg7289_cs         PORTC.3
#define zlg7289_clk        PORTC.4
#define zlg7289_dat        PORTC.5


#define zlg7289_reset 0xa4  //Zlgzlg7289 Reset Control Data
#define zlg7289_rdkey 0x15  //Zlgzlg7289 Get Key control Data

uchar disdata[4];
uint a=1500;
int b=100;
/**********************************
函数:zlg7289_writebyte()
功能:向zlg7289写入1个字节的数据
参数:
dat:要写入的数据
**********************************/
void zlg7289_writebyte(char dat)          
{
 uchar i;
 zlg7289_cs=0;
 zlg7289_clk=0;
 delay_us(50);
 for(i=0;i<8;i++)
 {
  zlg7289_dat=(dat&0x80)?1:0;
  //zlg7289_dat=(bit)(dat&0x80);
  zlg7289_clk=1;
  delay_us(50);
  zlg7289_clk=0;
  dat<<=1;
 }
 zlg7289_cs=1;
}

/**************************************
函数:zlg7289_write2bytes()
功能:向zlg7289总线写入2个字节的数据
返回:
***************************************/
void zlg7289_write2bytes(uchar addr,uchar dat)
{
 uchar i;
 zlg7289_cs=0;
 delay_us(50);
 for(i=0;i<8;i++)
 {
  zlg7289_dat=(addr&0x80)?1:0;
  //zlg7289_dat=(bit)(addr&0x80);
  zlg7289_clk=1;
  delay_us(50);
  zlg7289_clk=0;
  addr<<=1;
 }
 delay_us(1);
 for(i=0;i<8;i++)
 {
  zlg7289_dat=(dat&0x80)?1:0;
  //zlg7289_dat=(bit)(dat&0x80);
  zlg7289_clk=1;
  delay_us(50);
  zlg7289_clk=0;
  dat<<=1;
 }
 zlg7289_cs=1;
 delay_ms(10);
}
/**************************************
函数:zlg7289_init()
功能:初始化zlg7289芯片
返回:
***************************************/
void zlg7289_init()
{
 zlg7289_cs=1;
 zlg7289_clk=0;
 zlg7289_writebyte(zlg7289_reset);
}
/**************************************
函数:display()
功能:显示一个整型量
返回:无
***************************************/
void display(uint t1)
{
 disdata[0]=t1%10;      
  disdata[1]=t1%100/10;           
  disdata[2]=t1%1000/100;    
  disdata[3]=t1/1000;        
  
 zlg7289_write2bytes(0xc8,disdata[0]);   
 zlg7289_write2bytes(0xc9,disdata[1]);        
 zlg7289_write2bytes(0xca,disdata[2]);        
 zlg7289_write2bytes(0xcb,disdata[3]);  
}
// External Interrupt 1 service routine
interrupt [EXT_INT1] void ext_int1_isr(void)
{
// Place your code here

}

// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
// Reinitialize Timer 0 value
TCNT0=0x00;
// Place your code here

}

// Declare your global variables here

void main(void)
{
PORTB=0x00;
DDRB=0xFF;

PORTC=0x00;
DDRC=0x7F;

PORTD=0x00;
DDRD=0xF7;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 31.250 kHz
TCCR0=0x04;
TCNT0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 1000.000 kHz
// Mode: Fast PWM top=ICR1
// OC1A output: Non-Inv.
// 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=0x82;
TCCR1B=0x1A;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x4E;
ICR1L=0x20;
OCR1AH=0x05;
OCR1AL=0xDC;
OCR1BH=0x00;
OCR1BL=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: On
// INT1 Mode: Falling Edge
GICR|=0x80;
MCUCR=0x08;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x01;

#asm("sei")
zlg7289_init();
        while (1)
        {
               
                a=a+b;
                OCR1A=a;               
                display(a);
                if(a==2500)
                        b=-100;
                if(a==700)
                        b=100;
                delay_ms(500);
        }
}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
步进电机驱动程序
7279键盘显示程序
单片机常用程序有哪些?高级工考核反复使用的三个程序
STC单片机之PCA -- 单片机爱好者
51单片机液晶显示(含lcd12864和lcd1602)时钟_liutao2588的空间_...
STC15W404S单片机TM1650芯片驱动4位共阴数码管驱动程序
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服