Introduction to C8051F005 and DA/AD Program

Publisher:以泉换泉Latest update time:2021-04-29 Source: eefocusKeywords:C8051F005 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Introduction: This article mainly introduces the DA driver (C language) of C8051F005.


DA driver for C8051F005 (C language)

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

// 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

}

AD driver for C8051F005 (C language)

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

// 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 delay function

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(volatile 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_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; //Store the A/D conversion value in the display array

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_Heat

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

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) //Send command to 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_(); // Wait for writing to end

}

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

// 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{

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


}

}

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

[1] [2]
Keywords:C8051F005 Reference address:Introduction to C8051F005 and DA/AD Program

Previous article:Design and implementation of an embedded clock manager
Next article:51 Single Chip Microcomputer Frequency Counter Course Design

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号