C8051F005的简介及DA/AD程序

来源:本站
导读:目前正在解读《C8051F005的简介及DA/AD程序》的相关信息,《C8051F005的简介及DA/AD程序》是由用户自行发布的知识型内容!下面请观看由(电工技术网 - www.9ddd.net)用户发布《C8051F005的简介及DA/AD程序》的详细说明。
简介:本文主要介绍了C8051F005的DA驱动程序(C语言)。

C8051F005的DA驱动程序(C语言)

//-----------------------------------------------------------------------------

// Includes

//-----------------------------------------------------------------------------

#include // SFR declarations

#include

//-----------------------------------------------------------------------------

// 16-bit SFR Definitions for 'F00x

//-----------------------------------------------------------------------------

sfr16 DP = 0x82; // data pointer

sfr16 TMR3RL = 0x92; // Timer3 reload value

sfr16 TMR3 = 0x94; // Timer3 counter

sfr16 ADC0 = 0xbe; // ADC0 data

sfr16 ADC0GT = 0xc4; // ADC0 greater than window

sfr16 ADC0LT = 0xc6; // ADC0 less than window

sfr16 RCAP2 = 0xca; // Timer2 capture/reload

sfr16 T2 = 0xcc; // Timer2

sfr16 DAC0 = 0xd2; // DAC0 data

sfr16 DAC1 = 0xd5; // DAC1 data

//-----------------------------------------------------------------------------

// Function PROTOTYPES

//-----------------------------------------------------------------------------

void SYSCLK_Init (void);

//-----------------------------------------------------------------------------

// MAIN Routine

//-----------------------------------------------------------------------------

void main (void){

WDTCN = 0xde; // disable watchdog timer

WDTCN = 0xad;

SYSCLK_Init (); // initialize oscillator

REF0CN = 0x03; // Reference Control Register

//----------------------------------------------------------------------------

// DAC Configuration

//----------------------------------------------------------------------------

DAC0CN = 0x80; // DAC0 Control Register

DAC1CN = 0x80; // DAC1 Control Register

while(1)

{

DAC0L = 0xAB; // DAC0 Low Byte Register

DAC0H = 0x0C; // DAC0 High Byte Register

DAC1L = 0xAB; // DAC1 Low Byte Register

DAC1H = 0x0C; // DAC1 High Byte Register

}

}

//-----------------------------------------------------------------------------

// Initialization Subroutines

//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------

// Oscillator Configuration

//-----------------------------------------------------------------------------

void SYSCLK_Init (void)

{

int i; // delay counter

OSCXCN = 0x67; // start external oscillator with

// 18.432MHz crystal

for (i=0; i < 256; i++) ; // XTLVLD blanking interval (>1ms)

while (!(OSCXCN & 0x80)) ; // Wait for crystal osc. to settle

OSCICN = 0x88; // select external oscillator as SYSCLK

// source and enable missing clock

// detector

}

C8051F005的AD驱动程序(C语言)

//-----------------------------------------------------------------------------

// Includes

//-----------------------------------------------------------------------------

#include

#include

#include

//-----------------------------------------------------------------------------

// Function PROTOTYPES

//-----------------------------------------------------------------------------

#define uchar unsigned char

#define uint unsigned int

void SYSCLK_Init (void);

void delaynus(unsigned int q) ; //N us延时函数

void PORT_Init (void);

void SPI0_Init (void);

void LCD_Init(void);

void SendSPIByte(unsigned char ch);

void delaynms (unsigned int j);

void writecom(unsigned char com);

void writedata(unsigned char d);

void writechar(unsigned char ua);

void lcd_Net(void);

void Write_COM(uchar ins);

void SendSPIByte(uchar disdata);

void lcden(datad);

void LCD_set_xy( unsigned char x, unsigned char y );

void lcd_xhl(void);

void LCD_write_string(unsigned char n);

void lcd_adc(void);

void UART0_Init (void);

void ADC (void);

sbit lcdcs=P3^0;

//-----------------------------------------------------------------------------

// Global CONSTANTS

//-----------------------------------------------------------------------------

#define BAUDRATE 115200 // Baud rate of UART in bps

#define SYSCLK 11059200 // SYSCLK frequency in Hz

#define SAMPLE_RATE 50000 // Sample frequency in Hz

#define INT_DEC 4096 // integrate and decimate ratio

sfr16 DP = 0x82; // data pointer

sfr16 TMR3RL = 0x92; // Timer3 reload value

sfr16 TMR3 = 0x94; // Timer3 counter

sfr16 ADC0 = 0xbe; // ADC0 data

sfr16 ADC0GT = 0xc4; // ADC0 greater than window

sfr16 ADC0LT = 0xc6; // ADC0 less than window

sfr16 RCAP2 = 0xca; // Timer2 capture/reload

sfr16 T2 = 0xcc; // Timer2

sfr16 DAC0 = 0xd2; // DAC0 data

sfr16 DAC1 = 0xd5; // DAC1 data

unsigned char xdata ADC1Data[4];

unsigned int xdata ad_arr[50];

unsigned char datad ,comd,kk,sdf,ppca;

unsigned char virt_port,v,b,m;

unsigned char lcd_data_count;

unsigned char *lcdpoint;

unsigned char qqq;

unsigned char data8;

unsigned int i;

unsigned int AD_MAXT;

unsigned int AD_MINT;

long AD_VAL,AD_VAL_TEST;

unsigned char YYY;

long result;

long fff;

long hhh;

int j,w,z; // temperature in hundredths of a

unsigned int fff_int, fff_frac;

unsigned char p=0,a=0,s,d,f,g=0;

//-----------------------------------------------------------------------------

// MAIN Routine

//-----------------------------------------------------------------------------

void main(void){

WDTCN = 0xde; // disable watchdog timer

WDTCN = 0xad;

SYSCLK_Init ();

PORT_Init ();

UART0_Init() ; // initialize crossbar and GPIO

SPI0_Init ();

LCD_Init() ;

delaynms(100);

Write_COM(0x00);

delaynms(100);

delaynms(100);

Write_COM(0x01);

delaynms(100);

delaynus (100);

LCD_set_xy(0X00,0);

delaynus (200);

lcd_adc();

delaynus (200);

LCD_write_string(5);

delaynms (100);

while (1)

{

delaynms (100);

ADC ();

}

}

//-----------------------------------------------------------------------------

// Initialization Subroutines

//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------

// ADC

//-----------------------------------------------------------------------------

void ADC (void)

{

AMX0CF = 0x60;// AMUX Configuration Register

AMX0SL = 0x00;// AMUX Channel Select Register

ADC0CN = 0x00;// ADC Control Register

REF0CN = 0x03;

ADC0CN |= 0x80;

AD_VAL=0;

AD_VAL_TEST = 0;

for( YYY = 0;YYY<50;YYY++)

{

ad_arr[YYY]=0;

}

YYY=0;

ADCINT = 0;

for(YYY= 0;YYY<50;YYY++) //AD_TIMES=50

{

ADBUSY = 1;

while (!ADCINT);

ADCINT = 0;

_nop_();

_nop_();

_nop_(); //*AD_POINTER = ADC0

ad_arr[YYY]=ADC0;

}

for(YYY=0;YYY<50;YYY++)// for(i = 0;i<ad_times;i++)

{

AD_VAL_TEST += ad_arr[YYY];

}

AD_VAL = (int)(AD_VAL_TEST/50);

_nop_();

_nop_();

_nop_();

_nop_();

hhh=AD_VAL*5;

hhh=hhh*5;

hhh=hhh*100;

hhh=hhh/4096;

ADC1Data[0]=hhh/1000; //将A/D转化值存到显示数组

ADC1Data[1]=(hhh-1000*ADC1Data[0])/100;

ADC1Data[2]=(hhh-1000*ADC1Data[0]-100*ADC1Data[1])/10;

ADC1Data[3]=hhh-1000*ADC1Data[0]-100*ADC1Data[1]-10*ADC1Data[2];

delaynus (100);

LCD_set_xy(0X05,0);

delaynus (200);

for(qqq=0;qqq<4;qqq++)

{

data8=ADC1Data[qqq];

if(data8>=0x0a) data8 += 0x37;

else data8 += 0x30;

delaynms (200);

writechar(data8);

}

AD_VAL=0;

}

//-----------------------------------------------------------------------------

// PORT_Init

//-----------------------------------------------------------------------------

void PORT_Init (void)

{

XBR0 = 0x27; // XBAR0: Initial Reset Value

XBR1 = 0x00; // XBAR1: Initial Reset Value

XBR2 = 0x5c; // XBAR2: Initial Reset Value

PRT0CF = 0x14; // Output configuration for P0

PRT1CF = 0x10; // Output configuration for P3

PRT3CF = 0x01; // Output configuration for P3

}

//-----------------------------------------------------------------------------

// SYSCLK_Init

//-----------------------------------------------------------------------------

void SYSCLK_Init (void)

{

// delaynms counter

OSCXCN = 0x67; // start external oscillator with

for (i=0; i < 256; i++) ; // XTLVLD blanking interval (>1ms)

while (!(OSCXCN & 0x80)) ; // Wait for crystal osc. to settle

OSCICN = 0x88; // select external oscillator as SYSCLK

}

//-----------------------------------------------------------------------------

// SPI0_Init

//-----------------------------------------------------------------------------

void SPI0_Init (void)

{

SPI0CFG = 0x07; // data sampled on 1st SCK rising edge

SPI0CFG|=0xC0; //CKPOL =1;

SPI0CN = 0x03; // Master mode; SPI enabled; flags

SPI0CKR = SYSCLK/2/2000000-1; // SPI clock <= 8MHz (limited by

// EEPROM spec.)

}

//-----------------------------------------------------------------------------

// UART0_Init

//-----------------------------------------------------------------------------

void UART0_Init (void)

{

SCON = 0x50; // SCON: mode 1, 8-bit UART, enable RX

TMOD = 0x20; // TMOD: timer 1, mode 2, 8-bit reload

TH1 = -(SYSCLK/BAUDRATE/16); // set Timer1 reload value for baudrate

TR1 = 1; // start Timer1

CKCON |= 0x10; // Timer1 uses SYSCLK as time base

PCON |= 0x80; // SMOD = 1

TI = 1; // Indicate TX ready

}

//-----------------------------------------------------------------------------

// LCD_Init

//-----------------------------------------------------------------------------

void LCD_Init(void) //向LCD送命令

{

// unsigned int xdata x;

delaynms(100);

datad=0x00;

SendSPIByte(datad);

delaynms(10);

Write_COM(0x30);

delaynms(10);

Write_COM(0x30);

delaynms(10);

Write_COM(0x30);

delaynms(10) ;

Write_COM(0x28);

delaynms(100);

virt_port=0;

SendSPIByte(virt_port);

lcden(virt_port);

Write_COM(0x01);

delaynms(100);

Write_COM(0x06);

delaynms(10) ;

Write_COM(0x0C);

delaynms(500) ;

}

//-----------------------------------------------------------------------------

// SendSPIByte

//-----------------------------------------------------------------------------

void SendSPIByte(unsigned char ch)

{ lcdcs=1;

delaynus(100);

SPIF = 0;

SPI0DAT = ch;

while (SPIF == 0);

delaynus(100);

lcdcs=0;

delaynus(100);

_nop_(); // 等待写结束

}

//-----------------------------------------------------------------------------

// lcden

//-----------------------------------------------------------------------------

void lcden(datad)

{

datad|=0x08;

SendSPIByte(datad);

datad&=0xf7;

SendSPIByte(datad);

}

//-----------------------------------------------------------------------------

// delaynms

//-----------------------------------------------------------------------------

void delaynms (unsigned int uu)

{

unsigned int oo,ll;

for (oo=0;oo<uu;oo++)

{

for(ll=0;ll<1140;ll++);

}

}

//-----------------------------------------------------------------------------

// writechar

//-----------------------------------------------------------------------------

void writechar(unsigned char ua)

{

uint j;

uchar t,x;

for(j=0;j<500;j++);

datad|=0x02;

SendSPIByte(datad);

datad|=ua&0xf0;

SendSPIByte(datad);

datad|=0x08;

SendSPIByte(datad);

for(x=0;x<3;x++);

datad&=0xf7;

SendSPIByte(datad);

for(x=0;x<3;x++);

datad&=0x07;

delaynus(100);

SendSPIByte(virt_port);

t|=ua&0x0f;

datad|=t<<4;

SendSPIByte(datad);

for(x=0;x<3;x++);

datad|=0x08;

SendSPIByte(datad);

for(x=0;x<3;x++);

datad&=0xf7;

SendSPIByte(datad);

for(x=0;x<3;x++);

datad=0x00;

t=0x00;

SendSPIByte(datad);

}

//-----------------------------------------------------------------------------

// Write_COM

//-----------------------------------------------------------------------------

void Write_COM(uchar ins)

{

uchar t;

uint j;

for(j=0;j<5000;j++); //用延时代替查询

virt_port|=ins&0xf0;

SendSPIByte(virt_port);

//LCDE=1;

virt_port|=0x08;

SendSPIByte(virt_port);

for(i=3;i>0;i--);

//LCDE=0;

virt_port&=~0x08;

SendSPIByte(virt_port);

virt_port&=0x07;

SendSPIByte(virt_port);

t=ins<<4;

virt_port|=t&0xf0;

SendSPIByte(virt_port);

//LCDE=1;

virt_port|=0x08;

SendSPIByte(virt_port);

for(i=3;i>0;i--);

//LCDE=0;

virt_port&=~0x08;

SendSPIByte(virt_port);

virt_port=0;

SendSPIByte(virt_port);

}

//-----------------------------------------------------------------------------

// LCD_set_xy

//-----------------------------------------------------------------------------

void LCD_set_xy( unsigned char x, unsigned char y )

{

unsigned char address;

if (y == 0) address = 0x80 + x;

else

address = 0xc0 + x;

Write_COM(address);

}

//-----------------------------------------------------------------------------

// LCD_write_string

//-----------------------------------------------------------------------------

void LCD_write_string(unsigned char n)

{

unsigned char data1;

for(n;n>0;n--)

{

data1=*lcdpoint;

writechar(data1);

delaynms(100);

lcdpoint++;

delaynus(10);

}

}

//-----------------------------------------------------------------------------

// delaynus

//-----------------------------------------------------------------------------

void delaynus(unsigned int q) //N us延时函数

{

for (i=0;i<q;i++)

{

_nop_();

}

}

//-----------------------------------------------------------------------------

// lcd_adc

//-----------------------------------------------------------------------------

void lcd_adc(void)

{

unsigned char xdata ADC0Data[5]=;

lcdpoint=&ADC0Data;

}

利用C8051F005的DA,AD通过HD7279实现数据处理类问题(C语言)

//-----------------------------------------------------------------------------

// Includes

//-----------------------------------------------------------------------------

#include

#include

#include

//-----------------------------------------------------------------------------

// Function PROTOTYPES

//-----------------------------------------------------------------------------

#define uchar unsigned char

#define uint unsigned int

void SYSCLK_Init (void);

void PORT_Init (void);

void DAC (void);

void ADC (void);

void long_delay(void);// 长延时

void short_delay(void);// 短暂延时

void delay10ms(unsigned char);// 延时10MS

void delaynus(unsigned int q) ; //N us延时函数

void delaynms (unsigned int j);

void write7279(unsigned char, unsigned char);// 写入到HD7279

unsigned char read7279(unsigned char command);

void send_byte(unsigned char);// 发送一个字节

unsigned char receive_byte(void);

void DA_dispdata(void);

void AD_dispdata(void);

void dispStep();

void dispReset();

//-----------------------------------------------------------------------------

// Global CONSTANTS

//-----------------------------------------------------------------------------

#define BAUDRATE 115200 // Baud rate of UART in bps

#define SYSCLK 11059200 // SYSCLK frequency in Hz

#define SAMPLE_RATE 50000 // Sample frequency in Hz

#define INT_DEC 4096 // integrate and decimate ratio

sfr16 DP = 0x82; // data pointer

sfr16 TMR3RL = 0x92; // Timer3 reload value

sfr16 TMR3 = 0x94; // Timer3 counter

sfr16 ADC0 = 0xbe; // ADC0 data

sfr16 ADC0GT = 0xc4; // ADC0 greater than window

sfr16 ADC0LT = 0xc6; // ADC0 less than window

sfr16 RCAP2 = 0xca; // Timer2 capture/reload

sfr16 T2 = 0xcc; // Timer2

sfr16 DAC0 = 0xd2; // DAC0 data

sfr16 DAC1 = 0xd5; // DAC1 data

sbit cs=P3^2;// cs at P1.4

sbit clk=P3^3;// clk 连接于 P1.5

sbit dat=P3^1;// dat 连接于 P1.2

sbit key=P1^0;// key 连接于 P1.3

unsigned int xdata ad_arr[50];

unsigned char datad ,comd,kk,sdf,ppca;

unsigned char virt_port,v,b,m;

unsigned int tmr,ig,is,ib,iq,rg,rs,rb,rq;

unsigned char qqq;

unsigned char data8;

unsigned int i;

unsigned int AD_MAXT;

unsigned int AD_MINT;

long AD_VAL,AD_VAL_TEST;

unsigned char YYY;

long result;

long fff;

long hhh,ddd;

int j,w,z; // temperature in hundredths of a

unsigned int fff_int, fff_frac;

unsigned char p=0,a=0,s,d,f,g=0;

data unsigned char digit[5];

data unsigned char key_number, e, k;

unsigned long cnter;

unsigned long CurValue;

unsigned char CurStepLen;

unsigned long Decvalue0;

float Decvalue1,Decvalue2,Decvalue3;

/****** HD7279A 指令 ******/

#define CMD_RESET 0xa4

#define CMD_TEST 0xbf

#define DECODE0 0x80

#define DECODE1 0xc8

#define UNDECODE 0x90

#define RLC 0xa3

#define RRC 0xa2

#define RL 0xa1

#define RC 0xa0

#define HIDE 0x98

#define FLASH 0x88

#define SEGON 0xe0

#define SEGOFF 0xc0

#define CMD_READ 0x15

/***Main***/

void main(void)

{

WDTCN = 0xde; // disable watchdog timer

WDTCN = 0xad;

SYSCLK_Init ();

PORT_Init ();

cnter=0;

Decvalue0 = 0;

CurStepLen = 1;

for (tmr=0;tmr<0x2000;tmr++);// 上电延时

send_byte(CMD_RESET);

write7279(FLASH,0XFF);

write7279(HIDE,0x0f);

while (1)

{

key_number=0xff;

write7279(FLASH,0xf0);// 第1、2两位设为闪烁显示

write7279(UNDECODE ,0X08);// 在第1位显示下划线'_'

write7279(UNDECODE+1,0x08);// 在第2位显示下划线'_'

write7279(UNDECODE+2,0X08);

write7279(UNDECODE+3,0X08);

do

{

if (!key)// 如果有键按下

IE=0x81;

{

key_number=read7279(CMD_READ);// 读出键码

if (key_number < 0x0a) //0~9输入

{

digit[cnter] = key_number;

Decvalue0 = Decvalue0*10+digit[cnter];

send_byte(RLC); // delay10ms(12);

for(e=0;e<=cnter;e++)

{

write7279(DECODE1+3-e,digit[e]&0x0f);//delay10ms(5)

}

if (cnter==3)

{

DA_dispdata();

cnter=0;

}

else

}

}

if (key_number==0x0a) //ADD

{

Decvalue0 =Decvalue0 + CurStepLen;

DA_dispdata();

}

if (key_number==0x0b) //Dec

{

if (Decvalue0 > CurStepLen)

{

Decvalue0 =Decvalue0 - CurStepLen;

DA_dispdata();

}

}

if (key_number==0x0C) //Clear

{

send_byte(CMD_RESET);

write7279(HIDE,0x0f);

Decvalue0 = 0;

cnter=0;

dispReset();

}

if (key_number==0x0d) //Ok

{

write7279(FLASH,0xff);

DAC();// 清除闪烁设置

}

if (key_number==0x0E) // Change Step Len

{

write7279(FLASH,0xf0);

if ( CurStepLen ==1)

{

CurStepLen =10;

}

else{ CurStepLen =1;}

dispStep();

delay10ms(100);

write7279(FLASH,0xff);// DAData = CurValue;

}

if (key_number==0x0F) //Set Current Value

{

write7279(HIDE,0x0f);

Decvalue0 =0;

cnter=0;

write7279(FLASH,0xf0);// 第1、2两位设为闪烁显示

for (e=0;e<4;e++)

write7279(UNDECODE+e,0X08); // 在第1位显示下划线'_'

// dispStep();

// DAData = CurValue;

}

key_number = 0xff;

while (!key);

IE=0x00;

} while(1);// 等待按键放开

}

}

/***DAC***/

void DAC (void)

{

REF0CN = 0x03; // Reference Control Register

DAC0CN = 0x80; // DAC0 Control Register

DAC0L = 0xFF; // DAC0 Low Byte Register

DAC0H = 0x0F; %2

C8051F005的中文数据简介

模拟外设

8051 兼容的微控制器内核

100KSPS ADC

12 位 流水线指令结构 70%的指令的执行时间为一个或两 1LSB INL

个系统时钟周期 无失码

时钟频率为 25MHz时 速度可达 25MIPS 可编程转换速率 增强的中断系统

最大 100ksps 可有最多 22个中断源

可编程为单端输入或差分输入 8个外部输入 存储器

16 8 4 2 1 0.5 可编程放大器增益 2304字节数据存储器 数据相关窗口中断发生器 32K 字节闪速存储器 可以在系统编程扇区大小为 内置温度传感器 3°C

512字节

两个 12 位 DAC

数字外设 电压输出 32个 I/O口线 所有口线均容许 5V电压

2 TM TM TM 10微秒建立时间

C /SMBus SPI 可同时使用的硬件 I 及 UART串

两个比较器

口 16个可编程滞回电压值 16 位可编程的计数器/定时器阵列 带 5 个捕获/比较可用于产生中断或复位

模块 每一个都可配置为 8位 PWM

基准源 4个通用 16位计数器/定时器 2.4V

15ppm/°C 双向复位 专用的看门狗定时器外部基准输入 时钟源

VDD 监视器和节电降压检测器 内部可编程振荡器 2-16MHz

片内 JTAG 仿真 外部振荡器 晶体 RC C 或外部时钟 片内 JTAG 仿真电路提供全速 非侵入式的电路内 节电模式时使用 可在运行中切换时钟源

仿真 供电电压………………………………2.7V - 3.6V 典型工作电流 支持断点

单步 观察点 12mA @ 25MHz 观察/修改存储器和寄存器 多种节电休眠和停机模式 比使用仿真芯片 目标仿真头和仿真插座的仿真系 64 脚 TQFP 封装

-40°C - +85°C

统有更好的性能 温度范围

提醒:《C8051F005的简介及DA/AD程序》最后刷新时间 2024-03-14 01:07:59,本站为公益型个人网站,仅供个人学习和记录信息,不进行任何商业性质的盈利。如果内容、图片资源失效或内容涉及侵权,请反馈至,我们会及时处理。本站只保证内容的可读性,无法保证真实性,《C8051F005的简介及DA/AD程序》该内容的真实性请自行鉴别。