Safetycopy.c
Safetycopy.c
h>
#include <stdio.h>
XBR1 = 0X00;
XBR2 = 0x40; // Enable crossbar and weak pull-ups
#if (((SYSCLK/BAUDRATE)/(2L*12L))>0xFFL)
#error Timer 0 reload value is incorrect because
((SYSCLK/BAUDRATE)/(2L*12L))>0xFF
#endif
// Configure Uart 0
SCON0 = 0x10;
CKCON0 |= 0b_0000_0000 ; // Timer 1 uses the system clock divided by 12.
TH1 = 0x100-((SYSCLK/BAUDRATE)/(2L*12L));
TL1 = TH1; // Init Timer1
TMOD &= ~0xf0; // TMOD: timer 1 in 8-bit auto-reload
TMOD |= 0x20;
TR1 = 1; // START Timer1
TI = 1; // Indicate TX0 ready
return 0;
}
WriteCommand(line==2?0xc0:0x80);
waitms(5);
for(j=0; string[j]!=0; j++) WriteData(string[j]);// Write the message
if(clear) for(; j<CHARS_PER_LINE; j++) WriteData(' '); // Clear the rest of
the line
}
ADC0CN1=
(0x2 << 6) | // 0x0: 10-bit, 0x1: 12-bit, 0x2: 14-bit
(0x0 << 3) | // 0x0: No shift. 0x1: Shift right 1 bit. 0x2: Shift right 2
bits. 0x3: Shift right 3 bits.
(0x0 << 0) ; // Accumulate n conversions: 0x0: 1, 0x1:4, 0x2:8, 0x3:16,
0x4:32
ADC0CF0=
((SYSCLK/SARCLK) << 3) | // SAR Clock Divider. Max is 18MHz. Fsarclk =
(Fadcclk) / (ADSC + 1)
(0x0 << 2); // 0:SYSCLK ADCCLK = SYSCLK. 1:HFOSC0 ADCCLK = HFOSC0.
ADC0CF1=
(0 << 7) | // 0: Disable low power mode. 1: Enable low power mode.
(0x1E << 0); // Conversion Tracking Time. Tadtk = ADTK / (Fsarclk)
ADC0CN0 =
(0x0 << 7) | // ADEN. 0: Disable ADC0. 1: Enable ADC0.
(0x0 << 6) | // IPOEN. 0: Keep ADC powered on when ADEN is 1. 1: Power
down when ADC is idle.
(0x0 << 5) | // ADINT. Set by hardware upon completion of a data
conversion. Must be cleared by firmware.
(0x0 << 4) | // ADBUSY. Writing 1 to this bit initiates an ADC
conversion when ADCM = 000. This bit should not be polled to indicate when a
conversion is complete. Instead, the ADINT bit should be used when polling for
conversion completion.
(0x0 << 3) | // ADWINT. Set by hardware when the contents of
ADC0H:ADC0L fall within the window specified by ADC0GTH:ADC0GTL and
ADC0LTH:ADC0LTL. Can trigger an interrupt. Must be cleared by firmware.
(0x0 << 2) | // ADGN (Gain Control). 0x0: PGA gain=1. 0x1: PGA
gain=0.75. 0x2: PGA gain=0.5. 0x3: PGA gain=0.25.
(0x0 << 0) ; // TEMPE. 0: Disable the Temperature Sensor. 1: Enable the
Temperature Sensor.
ADC0CF2=
(0x0 << 7) | // GNDSL. 0: reference is the GND pin. 1: reference is the
AGND pin.
(0x1 << 5) | // REFSL. 0x0: VREF pin (external or on-chip). 0x1: VDD
pin. 0x2: 1.8V. 0x3: internal voltage reference.
(0x1F << 0); // ADPWR. Power Up Delay Time. Tpwrtime = ((4 * (ADPWR +
1)) + 2) / (Fadcclk)
ADC0CN2 =
(0x0 << 7) | // PACEN. 0x0: The ADC accumulator is over-written. 0x1:
The ADC accumulator adds to results.
(0x0 << 0) ; // ADCM. 0x0: ADBUSY, 0x1: TIMER0, 0x2: TIMER2, 0x3:
TIMER3, 0x4: CNVSTR, 0x5: CEX5, 0x6: TIMER4, 0x7: TIMER5, 0x8: CLU0, 0x9: CLU1,
0xA: CLU2, 0xB: CLU3
mask=1<<pinno;
SFRPAGE = 0x20;
switch (portno)
{
case 0:
P0MDIN &= (~mask); // Set pin as analog input
P0SKIP |= mask; // Skip Crossbar decoding for this pin
break;
case 1:
P1MDIN &= (~mask); // Set pin as analog input
P1SKIP |= mask; // Skip Crossbar decoding for this pin
break;
case 2:
P2MDIN &= (~mask); // Set pin as analog input
P2SKIP |= mask; // Skip Crossbar decoding for this pin
break;
default:
break;
}
SFRPAGE = 0x00;
}
float get_halfperiod_P0_1()
{
// Measure half period at pin P0.1 using timer 0
TR0 = 0;
TMOD&=0b_1111_0000; // Set the bits of Timer/Counter 0 to zero
TMOD|=0b_0000_0001; // Timer/Counter 0 used as a 16-bit timer
TR0=0; // Stop Timer/Counter 0
TH0=0;
TL0=0; // Reset the timer
TF0=0;
float get_halfperiod_P0_2()
{
unsigned int overflow_count=0;
TR0 = 0;
TMOD&=0b_1111_0000; // Set the bits of Timer/Counter 0 to zero
TMOD|=0b_0000_0001; // Timer/Counter 0 used as a 16-bit timer
TR0=0; // Stop Timer/Counter 0
TH0=0;
TL0=0; // Reset the timer
TF0=0;
// initalize timer 0
TR0 = 0;
TMOD &= 0xF0;
TMOD |= 0x01;
TH0 = 0;
TL0 = 0;
TF0 = 0;
LCD_4BIT();
//TIMER0_Init();
InitADC();
while(1)
{
// Read 14-bit value from the pins configured as analog inputs
v[0] = Volts_at_Pin(QFP32_MUX_P2_2);
v[1] = Volts_at_Pin(QFP32_MUX_P2_3);
v[2] = Volts_at_Pin(QFP32_MUX_P2_4);
v[3] = Volts_at_Pin(QFP32_MUX_P2_5);
period_diff = getPeriodDiff(2*(halfperiod_ref));