#include <mbed.h>
#include <stdio.h>
#include <string.h>
#include "platform/mbed_thread.h"
#include <BufferedSerial.h>
#include "my_mbed_fnd_lib.h"
#define UART2_TX_PIN PA_2
#define UART2_RX_PIN PA_3
static UnbufferedSerial rtx(UART2_TX_PIN, UART2_RX_PIN, 9600);
DigitalOut led1(PA_5, 1);
DigitalOut led2(PA_6, 1);
DigitalOut led3(PA_7, 1);
DigitalOut led4(PB_6, 1);
// tick timer 클래스 객체선언
Ticker my_tick_1; // ch1 읽기
Ticker my_tick_2; // ch 2 읽기
Ticker my_tick_3; // 30ms = fnd display
Ticker my_tick_4; // 340mscnt ++
// adc value define
struct ad
{
long ad_sum;
float volt;
float ad_buf;
uint16_t ad_hex;
uint8_t ad_flag;
char ad_dis[30];
}adc = {0,0,0,0,0};
// adc 객체선언
AnalogIn VR(PA_0, 3.3); // A0
AnalogIn VR_2(PA_1); //A1); // A0
uint16_t cnt = 1234;
uint8_t digit_cnt = 0;
uint8_t ad_ch_flag = 0;
void my_time_3()
{
// 30ms
digit_cnt++;
if(digit_cnt > 4) digit_cnt = 1;
fnd_595_out(digit_cnt, cnt);
}
void my_time_4()
{
// 1ms
//cnt++;
if(cnt> 9999) cnt = 0;
}
void ad_dis(char ch)
{
if(ch == 1 )
{
adc.volt = VR.read() * 3.3f; // 전압이 됨
adc.ad_sum = adc.volt * 1000;
sprintf(adc.ad_dis,"Volt_1 = %ld.%ld%ld%ld Volt,\n", adc.ad_sum/1000, adc.ad_sum%1000/100, adc.ad_sum%100/10, adc.ad_sum%10);
}
else
{
adc.volt = VR_2.read() * 3.3f; // 전압이 됨
adc.ad_sum = adc.volt * 1000;
sprintf(adc.ad_dis,"Volt_2 = %ld.%ld%ld%ld Volt,\n", adc.ad_sum/1000, adc.ad_sum%1000/100, adc.ad_sum%100/10, adc.ad_sum%10);
}
rtx.write(adc.ad_dis, 22);
ad_ch_flag = 0;
}
void ad_ch_1()
{
//400ms
led1 = !led1;
if(ad_ch_flag == 0) ad_ch_flag = 1;
}
void ad_ch_2()
{
led2 = !led2;
if(ad_ch_flag == 0) ad_ch_flag = 2;
}
int main() {
// put your setup code here, to run once:
// my_tick_3.attach(&my_time_3, 0.01); // 30mssec
// my_tick_4.attach(&my_time_3, 1.0); // 340mssec
my_tick_1.attach(&ad_ch_1, 0.8);
my_tick_2.attach(&ad_ch_2, 1.0);
while(1)
{
if(ad_ch_flag != 0) ad_dis(ad_ch_flag);
//if(ad_ch_flag != 0) ad_dis(1);
/*
// 실습-1
// put your main code here, to run repeatedly:
adc.ad_buf = VR.read(); // org Low Data
adc.volt = VR.read() * 3.3f; // 전압이 됨
//adc.volt = adc.ad_buf * 3.3f;
// 정규화 변환
// org Low Data
adc.ad_sum = adc.ad_buf * 1000;
sprintf(adc.ad_dis, "Org Low Data = .%3ld,\n", adc.ad_sum);
rtx.write(adc.ad_dis, sizeof(adc.ad_dis));
// Low Data 4096 단계로 표시
adc.ad_sum = adc.ad_buf * 4096;
sprintf(adc.ad_dis, "ADC Low Data = %4ld,\n", adc.ad_sum);
rtx.write(adc.ad_dis, 20);
// 전압표시
adc.ad_sum = adc.volt * 1000;
sprintf(adc.ad_dis,"Volt = %ld.%ld%ld%ld Volt,\n", adc.ad_sum/1000, adc.ad_sum%1000/100, adc.ad_sum%100/10, adc.ad_sum%10);
rtx.write(adc.ad_dis, 19);
// 백분율 표시
adc.ad_sum = adc.volt * 100.0f;
sprintf(adc.ad_dis, "x100 = %ld%ld.%ld %%\n", adc.ad_sum/100, adc.ad_sum%100/10, adc.ad_sum%10);
rtx.write(adc.ad_dis, 13);
*/
thread_sleep_for(500); // 500ms
}
}