📄 serialtest.c
字号:
//------------------------------------------------------------------------------------
// Includes
//------------------------------------------------------------------------------------
#include <c8051f000.h> // SFR declarations
#include <stdio.h>
#include <intrins.h>
//------------------------------------------------------------------------------------
// Global CONSTANTS
//------------------------------------------------------------------------------------
#define N 8
#define INBUF_LEN 4
#define SENDBUF_LEN 9
typedef unsigned char BYTE;
typedef unsigned int WORD;
typedef bit BOOL ;
sbit rs = P3^1; //lcd
sbit rw = P3^0;
sbit ep = P2^1;
sbit clk= P2^0;
sbit din= P1^4;
sbit led_cc=P1^0; //led
sbit led_cv=P1^1;
sbit led_cp=P1^2;
sbit led_cr=P1^3;
sbit key_adjust=P0^3; //key
sbit key_up=P3^6;
sbit key_down=P3^7;
sfr16 ADC0VAL=0xbe;
unsigned int kk;
unsigned char k=0;
BYTE code dis1[] = {"c3="};
BYTE code dis2[] = {"c4="};
BYTE code dis3[] = {"c1="};
BYTE code dis4[] = {"CC "};
BYTE code dis5[] = {"CV "};
BYTE code dis6[] = {"CP "};
BYTE code dis7[] = {"c2="};
BYTE code number[] = {0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
unsigned int c1_adc=0,c2_adc=0,c3_adc=0,c4_adc=0;
unsigned int c3_dis=0,c4_dis=0,c2_dis=0,c1_dis=0;
unsigned int voltage_set=0,current_set=0,power_set=0;
long voltage_da =0,current_da =0,power_da =0;
unsigned int voltage_dac=0,current_dac=0,power_dac=0;
unsigned int display_set=0;
unsigned int voltage_out=0;
bit flag_ad=0;
unsigned char sendbuf[SENDBUF_LEN];
unsigned char inbuf[INBUF_LEN];
unsigned char count=0;
unsigned char sum_check;
bit flag_set=0;
bit flag_end;
bit flag_false;
bit flag_command;
bit flag_data;
bit flag_handshake;
bit flag_len;
unsigned char handshaking;
unsigned char data_len;
//------------------------------------------------------------------------------------
// Config Routine
//------------------------------------------------------------------------------------
void config (void) {
//Local Variable Definitions
//----------------------------------------------------------------
// Watchdog Timer Configuration
//
// WDTCN.[7:0]: WDT Control
// Writing 0xA5 enables and reloads the WDT.
// Writing 0xDE followed within 4 clocks by 0xAD disables the WDT
// Writing 0xFF locks out disable feature.
//
// WDTCN.[2:0]: WDT timer interval bits
// NOTE! When writing interval bits, bit 7 must be a 0.
//
// Bit 2 | Bit 1 | Bit 0
//------------------------
// 1 | 1 | 1 Timeout interval = 1048576 x Tsysclk
// 1 | 1 | 0 Timeout interval = 262144 x Tsysclk
// 1 | 0 | 1 Timeout interval = 65636 x Tsysclk
// 1 | 0 | 0 Timeout interval = 16384 x Tsysclk
// 0 | 1 | 1 Timeout interval = 4096 x Tsysclk
// 0 | 1 | 0 Timeout interval = 1024 x Tsysclk
// 0 | 0 | 1 Timeout interval = 256 x Tsysclk
// 0 | 0 | 0 Timeout interval = 64 x Tsysclk
//
//------------------------
WDTCN = 0x07; // Watchdog Timer Control Register
WDTCN = 0xDE; // Disable WDT WDTCN = 0xAD;
//----------------------------------------------------------------
// CROSSBAR REGISTER CONFIGURATION
//
// NOTE: The crossbar register should be configured before any
// of the digital peripherals are enabled. The pinout of the
// device is dependent on the crossbar configuration so caution
// must be exercised when modifying the contents of the XBR0,
// XBR1, and XBR2 registers. For detailed information on
// Crossbar Decoder Configuration, refer to Application Note
// AN001, "Configuring the Port I/O Crossbar Decoder".
//----------------------------------------------------------------
// Configure the XBRn Registers
XBR0 = 0x04; // XBAR0: Initial Reset Value
XBR1 = 0x14; // XBAR1: Initial Reset Value
XBR2 = 0x40; // XBAR2: Initial Reset Value
// Select Pin I/0
// NOTE: Some peripheral I/O pins can function as either inputs or
// outputs, depending on the configuration of the peripheral. By default,
// the configuration utility will configure these I/O pins as push-pull
// outputs.
// Port configuration (1 = Push Pull Output)
PRT0CF = 0x03; // Output configuration for P0
PRT1CF = 0x00; // Output configuration for P1
PRT2CF = 0x00; // Output configuration for P2
PRT3CF = 0x00; // Output configuration for P3
// View port pinout
// The current Crossbar configuration results in the
// following port pinout assignment:
// Port 0
// P0.0 = UART TX (Push-Pull Output) // P0.1 = UART RX (Push-Pull Output) // P0.2 = /INT0 (Open-Drain Output/Input) // P0.3 = GP I/O (Open-Drain Output/Input) // P0.4 = GP I/O (Open-Drain Output/Input) // P0.5 = GP I/O (Open-Drain Output/Input) // P0.6 = GP I/O (Open-Drain Output/Input) // P0.7 = GP I/O (Open-Drain Output/Input)
// Port 1
// P1.0 = GP I/O (Open-Drain Output/Input) // P1.1 = GP I/O (Open-Drain Output/Input) // P1.2 = GP I/O (Open-Drain Output/Input) // P1.3 = GP I/O (Open-Drain Output/Input) // P1.4 = GP I/O (Open-Drain Output/Input) // P1.5 = GP I/O (Open-Drain Output/Input) // P1.6 = GP I/O (Open-Drain Output/Input) // P1.7 = GP I/O (Open-Drain Output/Input)
// Port 2
// P2.0 = GP I/O (Open-Drain Output/Input) // P2.1 = GP I/O (Open-Drain Output/Input) // P2.2 = GP I/O (Open-Drain Output/Input) // P2.3 = GP I/O (Open-Drain Output/Input) // P2.4 = GP I/O (Open-Drain Output/Input) // P2.5 = GP I/O (Open-Drain Output/Input) // P2.6 = GP I/O (Open-Drain Output/Input) // P2.7 = GP I/O (Open-Drain Output/Input)
// Port 3
// P3.0 = GP I/O (Open-Drain Output/Input) // P3.1 = GP I/O (Open-Drain Output/Input) // P3.2 = GP I/O (Open-Drain Output/Input) // P3.3 = GP I/O (Open-Drain Output/Input) // P3.4 = GP I/O (Open-Drain Output/Input) // P3.5 = GP I/O (Open-Drain Output/Input) // P3.6 = GP I/O (Open-Drain Output/Input) // P3.7 = GP I/O (Open-Drain Output/Input)
//----------------------------------------------------------------
// Comparators Register Configuration
//
// Bit 7 | Bit 6 | Bit 5 | Bit 4 | Bit 3 | Bit 2 | Bit 1 | Bit 0
//------------------------------------------------------------------
// R/W | R | R/W | R/W | R/W | R/W | R/W | R/W
//------------------------------------------------------------------
// Enable | Output | Rising | Falling| Positive | Negative
// | State | Edge | Edge | Hysterisis | Hysterisis
// | Flag | Int. | Int. | 00: Disable | 00: Disable
// | | Flag | Flag | 01: 5mV | 01: 5mV
// | | | | 10: 10mV | 10: 10mV
// | | | | 11: 20mV | 11: 20mV
// ----------------------------------------------------------------
CPT0CN = 0x00; // Comparator 0 Control Register
CPT1CN = 0x00; // Comparator 1 Control Register
//Comp1 marker
//----------------------------------------------------------------
// Oscillator Configuration
//----------------------------------------------------------------
OSCXCN = 0x30; // EXTERNAL Oscillator Control Register
OSCICN = 0x04; // Internal Oscillator Control Register
//----------------------------------------------------------------
// Reference Control Register Configuration
//----------------------------------------------------------------
REF0CN = 0x03; // Reference Control Register
//----------------------------------------------------------------
// SPI Configuration
//----------------------------------------------------------------
SPI0CN = 0x00; // SPI Control Register
SPI0CFG = 0x00; // SPI Configuration Register
SPI0CKR = 0x00; // SPI Clock Rate Register
//----------------------------------------------------------------
// DAC Configuration
//----------------------------------------------------------------
DAC0CN = 0x80; // DAC0 Control Register
DAC0L = 0xe0; // DAC0 Low Byte Register
DAC0H = 0x0a; // DAC0 High Byte Register
DAC1CN = 0x00; // DAC1 Control Register
DAC1L = 0x00; // DAC1 Low Byte Register
DAC1H = 0x00; // DAC1 High Byte Register
//----------------------------------------------------------------
// UART Configuration
//----------------------------------------------------------------
SCON = 0x50; // Serial Port Control Register
SCON &= 0xFC; //clear interrupt pending flags PCON = 0x00; // Power Control Register
//----------------------------------------------------------------
// SMBus Configuration
//----------------------------------------------------------------
SMB0CN = 0x00; // SMBus Control Register
SMB0ADR = 0x00; // SMBus Address Register
SMB0CR = 0x00; // SMBus Clock Rate Register
//----------------------------------------------------------------
// PCA Configuration
//----------------------------------------------------------------
PCA0MD = 0x00; // PCA Mode Register
PCA0CN = 0x00; // PCA Control Register
PCA0H = 0x00; // PCA Counter/Timer High Byte
PCA0L = 0x00; // PCA Counter/Timer Low Byte
//Module 0
PCA0CPM0 = 0x00; // PCA Capture/Compare Register 0
PCA0CPL0 = 0x00; // PCA Counter/Timer Low Byte
PCA0CPH0 = 0x00; // PCA Counter/Timer High Byte
//Module 1
PCA0CPM1 = 0x00; // PCA Capture/Compare Register 1
PCA0CPL1 = 0x00; // PCA Counter/Timer Low Byte
PCA0CPH1 = 0x00; // PCA Counter/Timer High Byte
//Module 2
PCA0CPM2 = 0x00; // PCA Capture/Compare Register 2
PCA0CPL2 = 0x00; // PCA Counter/Timer Low Byte
PCA0CPH2 = 0x00; // PCA Counter/Timer High Byte
//Module 3
PCA0CPM3 = 0x00; // PCA Capture/Compare Register 3
PCA0CPL3 = 0x00; // PCA Counter/Timer Low Byte
PCA0CPH3 = 0x00; // PCA Counter/Timer High Byte
//Module 4
PCA0CPM4 = 0x00; // PCA Capture/Compare Register 4
PCA0CPL4 = 0x00; // PCA Counter/Timer Low Byte
PCA0CPH4 = 0x00; // PCA Counter/Timer High Byte
//----------------------------------------------------------------
// ADC Configuration
//----------------------------------------------------------------
AMX0CF = 0x60; // AMUX Configuration Register
AMX0SL = 0x00; // AMUX Channel Select Register
ADC0CF = 0x80; // ADC Configuration Register
ADC0CN = 0xC0; // ADC Control Register
ADC0LTH = 0x00; // ADC Less-Than High Byte Register
ADC0LTL = 0x00; // ADC Less-Than Low Byte Register
ADC0GTH = 0xFF; // ADC Greater-Than High Byte Register
ADC0GTL = 0xFF; // ADC Greater-Than Low Byte Register
//----------------------------------------------------------------
// Timer Configuration
//----------------------------------------------------------------
CKCON = 0x00; // Clock Control Register
TH0 = 0x00; // Timer 0 High Byte
TL0 = 0x00; // Timer 0 Low Byte
TH1 = 0xFD; // Timer 1 High Byte
TL1 = 0xFD; // Timer 1 Low Byte
TMOD = 0x21; // Timer Mode Register
TCON = 0x05; // Timer Control Register
RCAP2H = 0x00; // Timer 2 Capture Register High Byte
RCAP2L = 0x00; // Timer 2 Capture Register Low Byte
TH2 = 0x00; // Timer 2 High Byte
TL2 = 0x00; // Timer 2 Low Byte
T2CON = 0x00; // Timer 2 Control Register
TMR3RLL = 0x00; // Timer 3 Reload Register Low Byte
TMR3RLH = 0x00; // Timer 3 Reload Register High Byte
TMR3H = 0x00; // Timer 3 High Byte
TMR3L = 0x00; // Timer 3 Low Byte
TMR3CN = 0x00; // Timer 3 Control Register
//----------------------------------------------------------------
// Reset Source Configuration
//
// Bit 7 | Bit 6 | Bit 5 | Bit 4 | Bit 3 | Bit 2 | Bit 1 | Bit 0
//------------------------------------------------------------------
// R | R/W | R/W | R/W | R | R | R/W | R
//------------------------------------------------------------------
// JTAG |Convert | Comp.0 | S/W | WDT | Miss. | POR | HW
// Reset |Start | Reset/ | Reset | Reset | Clock | Force | Pin
// Flag |Reset/ | Enable | Force | Flag | Detect| & | Reset
// |Enable | Flag | & | | Flag | Flag | Flag
// |Flag | | Flag | | | |
//------------------------------------------------------------------
// NOTE! : Comparator 0 must be enabled before it is enabled as a
// reset source.
//
// NOTE! : External CNVSTR must be enalbed through the crossbar, and
// the crossbar enabled prior to enabling CNVSTR as a reset source
//------------------------------------------------------------------
RSTSRC = 0x00; // Reset Source Register
//----------------------------------------------------------------
// Interrupt Configuration
//----------------------------------------------------------------
IE = 0x16; //Interrupt Enable
IP = 0x00; //Interrupt Priority
EIE1 = 0x00; //Extended Interrupt Enable 1
EIE2 = 0x02; //Extended Interrupt Enable 2
EIP1 = 0x00; //Extended Interrupt Priority 1
EIP2 = 0x00; //Extended Interrupt Priority 2
// other initialization code here...
} //End of config
delay(void)
{ // 延时子程序
unsigned char i;
for(i = 0; i< 25; i++);
}
delayms (unsigned char ms)
{
unsigned int i;
while(ms--)
{
for(i=0;i<2000;i++);
}
}
lcd_wcmd(BYTE cmd)
{ // 写入指令数据到LCD
unsigned char i;
rs = 0;
rw = 0;
ep = 0;
delay();
for(i=1;i<=8;i++)
{
din=cmd&0x80;
cmd=cmd<<1;
clk=0;clk=1;
}
ep = 1;
delay();
ep = 0;
}
lcd_pos(BYTE pos)
{ //设定显示位置
lcd_wcmd(pos | 0x80);
}
lcd_wdat(BYTE dat)
{ // 写入指令数据到LCD
unsigned char i;
rs = 1;
rw = 0;
ep = 0;
delay();
for(i=1;i<=8;i++)
{
din=dat&0x80;
dat=dat<<1;
clk=0;clk=1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -