0

关于投票
简易电压源

/*****************************************************
This program was produced by the
CodeWizardAVR V1.24.8c Professional
Automatic Program Generator
?Copyright 1998-2006 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project : 简易直流电压源
Version :
Date    : 2007-5-18
Author  : F4CG                           
Company : F4CG                           
Comments:


Chip type           : ATmega16L
Program type        : Application
Clock frequency     : 8.000000 MHz
Memory model        : Small
External SRAM size  : 0
Data Stack size     : 256
*****************************************************/

#include <mega16.h>
#include <delay.h>
#include "1602a.h"
#define ADC_VREF_TYPE 0xc7//内部基准电源,单通道输入PA7
unsigned int adc_v; 

int key_stime_ok,key_stime_counter;
void adc_to_disbuffer(unsigned int adc)
{
 // #asm("cli")
  
 //  LcdPos(6,0); //确定光标位置
  // LcdWd(adc%10+0x30);
   adc="adc/10";
   LcdPos(5,0); //确定光标位置
   LcdWd(adc%10+0x30);
   adc/=10;
     LcdPos(4,0); //确定光标位置
   LcdWd(adc%10+0x30);
   adc/=10; 
    LcdPos(3,0); //确定光标位置
   LcdWd('.');
     LcdPos(2,0); //确定光标位置
   LcdWd(adc%10+0x30);
   adc/=10;
//#asm("sei")
}   
interrupt [TIM0_COMP] void timer0_comp_isr(void)
{
// Place your code here   

if (++key_stime_counter >=1)
  { 
  key_stime_ok = 1; 
                  }
}
// ADC interrupt service routine
interrupt [ADC_INT] void adc_isr(void)
{
  unsigned int adc_data;
// Read the AD conversion result
adc_data=ADCW;

// Place your code here
adc_v=(unsigned long)adc_data*2560/1024;    //换算成电压值
  adc_to_disbuffer(adc_v);  
   
}

// Declare your global variables here

void main(void)
{
// Declare your local variables here
//char j="88";
// Input/Output Ports initialization
// Port A 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
PORTA=0x00;
DDRA=0x00;

// 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=0xff;//液晶接口
DDRB=0xff;

// Port C 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
PORTC=0x00;
DDRC=0xff;

// 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=0xff;     
DDRD=0xff;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top="FFh"
// OC0 output: Disconnected
TCCR0=0x0C;//采用比较模式,256分频   8M/256=31.25K
//TCCR0=0X0D;//1024   7.81k
TCNT0=0x00;
OCR0=0x40;// (61+1)/31.25   =2ms
 // OCR0=0XFF;
// 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
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;

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

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// ADC initialization
// ADC Clock frequency: 125.000 kHz
// ADC Voltage Reference: Int., cap. on AREF
// ADC Auto Trigger Source: Timer0 Compare Match
ADMUX=ADC_VREF_TYPE;
ADCSRA=0xAE;
SFIOR&=0x1F;
SFIOR|=0x60;

// Global enable interrupts

RstLcd();//液晶初始化
RstLcd();//液晶初始化
RstLcd();//液晶初始化
/*
LcdPos(0,0);//x,y
LcdWd(j%10+0x30);  
LcdPos(1,0);//x,y
j=j/10;
LcdWd(j%10+0x30);
//j=j/10 ;
LcdPos(2,0);//x,y
LcdWd('=');
LcdPos(3,0);
//Write_String(str);
lcd_putsf("JIANGLONGJIAN");
//LcdWd('b');*/
  LcdPos(0,0); //确定光标位置
  LcdWd('v');
  LcdPos(1,0); //确定光标位置
  LcdWd('=');
  #asm("sei")
while (1)
      {
      // Place your code here
   //   if (key_stime_ok==1)
    // { key_stime_ok=0;
     //  adc_to_disbuffer(adc_v);
      // } 
 
      };
}
c

系统分类: 汽车电子
用户分类: AVR单片机
标签: 简易电压源
来源: 原创
发表评论 阅读全文(270) | 回复(0)

0

关于投票
基于1302的万年历

这个是1302程序,液晶用12864来显示

rar

系统分类: 汽车电子
用户分类: AVR单片机
标签: 1302
来源: 原创
发表评论 阅读全文(304) | 回复(0)

0

关于投票
模拟SPI接口程序

//***********************************************************
//功能      : 由AVR作为主机,向FPGA发送数据,采用模拟SPI接口来实现
//            互相通信 
//作者      : 小白龙
//创建日期  : 2007.8.1 
//版本      :  V1.1
//修改日期  :2007.8.12
//************************************************************
#define clk_r       PORTD.0  //所有IO口初始时设为输出口
#define data_r      PORTD.1
#define ss_r        PORTD.2

#define clk_s       PORTD.3
#define ss_s        PORTD.4
#define data_s      PORTD.5
#define data_sPIN   PIND.5
#define data_sDDR   DDRD.5 
#define IO_PIN      PIND
#define IO_DDR      DDRD.1  //数据读入口
 uchar spiread(void);
void spisend(uchar data);


/**********************************************************
*
*功能:模拟SPI接口程序,低位先发送,最后发送高位,
      FPGA接收数据往左移动,即先接收高位 ,二者顺序发送和接收数据相反
*说明:在CLK的上升沿开始写入数据,开始时SS位片选信号,
      当SS由高变低时,产生时钟信号,开始传送
*********************************************************/
void spisend(uchar data)//写入
{  
    uchar i;
    data_sDDR=1;
    clk_s=0;
    for(i=8;i>0;i--)
 {
  if ((data&0x01)==1)
   {
   data_s=1;
   }
  else
   data_s=0;
   delay_1us();
  clk_s=1;
 // delay_1us();
  clk_s=0;  
  data>>=1;//右移一位
 }
}
/**********************************************************
*
*名称:spiread()
*功能:AVR作为主机,由AVR提供时仲,从FPGA取数据
*说明:在CLK的下降沿开始读数据,FPGA的数据从高位开始发送,
      AVR中接收的数据先送到低位中
*********************************************************/
uchar spiread(void)
{
 uchar i;
 uchar data="0x00";
 uchar temp; 
 IO_DDR=0;  //IO口作为输入,DDR要写0 
 ss_r=0;
 clk_r=0;  
 for(i=0;i<8;i++)
 {   
  clk_r=1;
  clk_r=0;     
  temp="IO"_PIN;
        if (temp&0x02)       //下降沿采集数据,调换则会出错
 data|=(0x01<<i);
 
 }
 IO_DDR=1;
 ss_r=1;  //读完数据后,SS拉高,同时时钟拉高一次,上升沿重新置数
 clk_r=1;
return(data);
}

// 
//
// PORTA="0x00";   //灯指示
// DDRA="0XFF";
//
// PORTB="0x00";//0101 0000
// DDRB="0x07";//0000 0111接口作为模拟SPI口输出接口要写1
//
// PORTC="0x00";
// DDRC="0x80";
//
// DDRD="0xff";//0000 0001
// PORTD="0x00";//0100 1000
//         // SPUL="1",CL=1;
//
// // Timer/Counter 0 initialization
// TCCR0=0x0b;//采用比较模式,256分频   8M/256=31.25K
// TCNT0=0x00;
// OCR0=0x3D;// (61+1)/31.25   =2ms
//
// // Timer/Counter 1 initialization
// TCCR1A=0x00;
// TCCR1B=0x00;
// TCNT1H=0x00;
// TCNT1L=0x00;
// ICR1H=0x00;
// ICR1L=0x00;
// OCR1AH=0x00;
// OCR1AL=0x00;
// OCR1BH=0x00;
// OCR1BL=0x00;
//
// // Timer/Counter 2 initialization
// ASSR="0x00";
// TCCR2=0x00;
// TCNT2=0x00;
// OCR2=0x00;
//
// // External Interrupt(s) initialization
// GICR|=0x40;
// MCUCR="0x03";
// MCUCSR="0x00";
// GIFR="0x40";     //通过写1来清0
// // Timer(s)/Counter(s) Interrupt(s) initialization
// TIMSK="0x02";
//
// // Analog Comparator initialization
// ACSR="0x80";
// SFIOR="0x00";
// ss_r=1;
// //PORTB.1=1;
// key_init(); //键盘初始化
// #asm("sei")   // 开放全局中断
//  
// while (1)
//       {
      // Place your code here
//***********************键盘处理*******************************//
//       
// if (key_stime_ok)    10ms到,键处理
//           {
//      key_stime_ok = 0;
//               switch (read_key())  调用按键接口程序  
//        {
//        case 1:
//    {
//     ss_r=0;  在接收数据时要先拉低
//     PORTA="spiread"();
//     ss_r=1;
//    clk_r=1; 
//            }  break; 
//         case 2:
//            {
//            ss_s=0;
//     spisend(0x80);
//     ss_s=1;
//            }break;
//         case 3:
//         {
//           ss_s=0;
//     spisend(0x00);
//     ss_s=1;
//            }
//            break;
//        case 4:
//        { 
//          }
//          break;
//          }
// }end keyproc
// 
// }end while
// }

系统分类: 汽车电子