📄 exercise-10.c
字号:
/* Exercise 10
A simple voltmeter demonstration.
*/
#include <stdint.h>
#include <msp430x42x0.h>
//defines
#define PB_2_0 (1 << 0) // Push Button on P2.0
#define PB_2_1 (1 << 1) // Push Button on P2.1
#define seg_a 0x01
#define seg_b 0x02
#define seg_c 0x10
#define seg_d 0x04
#define seg_e 0x80
#define seg_f 0x20
#define seg_g 0x08
#define seg_h 0x40
#define CHAR_0 (seg_a | seg_b | seg_c | seg_d | seg_e | seg_f)
#define CHAR_1 (seg_b | seg_c)
#define CHAR_2 (seg_a | seg_b | seg_d | seg_e | seg_g)
#define CHAR_3 (seg_a | seg_b | seg_c | seg_d | seg_g)
#define CHAR_4 (seg_b | seg_c | seg_f | seg_g)
#define CHAR_5 (seg_a | seg_c | seg_d | seg_f | seg_g)
#define CHAR_6 (seg_a | seg_c | seg_d | seg_e | seg_f | seg_g)
#define CHAR_7 (seg_a | seg_b | seg_c)
#define CHAR_8 (seg_a | seg_b | seg_c | seg_d | seg_e | seg_f | seg_g)
#define CHAR_9 (seg_a | seg_b | seg_c | seg_d | seg_f | seg_g)
#define CHAR_A (seg_a | seg_b | seg_c | seg_e | seg_f | seg_g)
#define CHAR_B (seg_c | seg_d | seg_e | seg_f | seg_g)
#define CHAR_C (seg_a | seg_d | seg_e | seg_f)
#define CHAR_D (seg_b | seg_c | seg_d | seg_e | seg_g)
#define CHAR_E (seg_a | seg_d | seg_e | seg_f | seg_g)
#define CHAR_F (seg_a | seg_e | seg_f | seg_g)
#define CHAR_SPACE 0
#define CHAR_MINUS seg_g
const unsigned char hex_table[] =
{
CHAR_0,
CHAR_1,
CHAR_2,
CHAR_3,
CHAR_4,
CHAR_5,
CHAR_6,
CHAR_7,
CHAR_8,
CHAR_9,
CHAR_A,
CHAR_B,
CHAR_C,
CHAR_D,
CHAR_E,
CHAR_F
};
#define _4052_A BIT5
#define _4052_B BIT4
long sample_accum;
int sample_count;
int read_channel(unsigned int count, unsigned char channel);
void LCDchar(int ch, int pos)
{
/* Put a segment pattern at a specified position on the LCD display */
LCDMEM[7 - pos] = ch;
}
#pragma vector=BASICTIMER_VECTOR
__interrupt void basic_timer_interrupt(void)
{
int next;
/* Step the output of the DAC. Do not let it get too high, or it
will be out of the ADC range for a long time. */
next = DAC12_0DAT += 1;
if (next > 0xC00)
next = 0;
DAC12_0DAT = next;
}
void init_basic_timer(void)
{
/* Basic timer setup */
/* Set ticker to 32768/(256*256) */
BTCTL = BT_fLCD_DIV64 | BT_fCLK2_DIV128 | BT_fCLK2_ACLK_DIV256;
/* Enable 1 second interrupt */
IE2 |= BTIE;
}
void init_fll(void)
{
int i;
FLL_CTL0 |= XCAP18PF; // Set load capacitance for xtal
while (LFOF & FLL_CTL0)
/*wait*/; // wait for watch crystal to stabilize
SCFQCTL = 63; // 32 x 32768 x 2 = 2.097152MHz
for (i = 0; i < 10000; i++)
/* wait */;
}
#pragma vector=SD16_VECTOR
__interrupt void sd16_interrupt(void)
{
}
void init_adc(void)
{
/* Initialize and enable the SD16 */
SD16CTL = SD16DIV_2 | SD16SSEL_1 | SD16REFON | SD16VMIDON;
SD16IV = 0;
SD16INCTL0 = SD16GAIN_1 | SD16INCH_0;
SD16AE = SD16AE2 | SD16AE3 | SD16AE4 | SD16AE6 | SD16AE7;
P6SEL |= (BIT3 | BIT2 | BIT1 | BIT0);
SD16CCTL0 = SD16BUF_1 | SD16OSR_128 | SD16UNI;
}
void init_lcd(void)
{
int j;
for (j = 0; j < 20; j++)
LCDMEM[j] = 0;
/* Turn on the COM0-COM3 and R03-R33 pins */
P5SEL |= (BIT7 | BIT6 | BIT5 | BIT4 | BIT3 | BIT2 | BIT1 | BIT0);
P5DIR |= (BIT4 | BIT3 | BIT2);
LCDACTL = LCDFREQ_128 | LCD4MUX | LCDSON | LCDON;
LCDAPCTL0 = LCDS0 | LCDS4 | LCDS8 | LCDS12;
LCDAPCTL1 = 0;
LCDAVCTL0 = LCDCPEN;
LCDAVCTL1 = 12 << 1;
}
int read_channel(unsigned int count, unsigned char channel)
{
unsigned int i;
unsigned long l;
SD16INCTL0 = SD16GAIN_4 | channel;
l = 0;
for(i = 0; i < count; i++)
{
SD16CCTL0 |= SD16SC;
while (!(SD16CCTL0 & SD16IFG))
/* wait */;
l += SD16MEM0;
}
return((int) (l/count));
}
void main(void)
{
unsigned int i;
unsigned int voltage;
unsigned long int scaled_voltage;
float volts;
WDTCTL = WDTPW | WDTHOLD;
init_fll();
P6DIR = 0xFF; // All P6.x outputs
P6OUT = _4052_A | _4052_B; // All P6.x reset
init_basic_timer();
init_lcd();
init_adc();
/* Configure DAC to generate a slowly stepping ramp */
DAC12_0CTL = DAC12OPS | DAC12CALON | DAC12IR | DAC12AMP_2 | DAC12SREF_2 | DAC12ENC; // DAC0 enable
DAC12_0DAT = 0;
P6OUT = _4052_A | _4052_B;
_EINT();
for (;;)
{
voltage = (unsigned int) read_channel(256, SD16INCH_0);
volts = (float) voltage*0.803*100000.0/65536.0;
scaled_voltage = volts;
if (scaled_voltage >= 79999)
{
/* Overrange */
for (i = 0; i < 5; i++)
LCDMEM[i] = CHAR_MINUS;
}
else
{
for (i = 0; i < 5; i++)
{
LCDMEM[i] = hex_table[scaled_voltage%10]; // remainder = character in table to display
scaled_voltage /= 10; // shifts right so next character can be displayed
}
LCDMEM[2] |= seg_h; // Decimal point
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -