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++); } } //-----------------------------------------------------------------------------
Previous article:Design and implementation of an embedded clock manager
Next article:51 Single Chip Microcomputer Frequency Counter Course Design
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Three steps to govern hybrid multicloud environments
- Three steps to govern hybrid multicloud environments
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- Unboxing ESP32-S2-Kaluga && Laser PM2.5 Sensor
- SD NAND and STM32 series successful experience sharing
- BK3432 BK3431Q Bluetooth ble chip Bluetooth smart lock application
- When the wifi module is not soldered, it will keep printing errors
- EAC0945 wince6.0 BSP compilation manual
- Question: How to handle power-off reset of MSP430G2553 microcontroller?
- How to judge the positive and negative turning angle in PID vehicle lateral control
- [GD32L233C-START Review] 2. Create a new project step by step
- Zigbee network data transmission method
- What exactly is op amp noise?