#include <mbed.h>
#include <stdio.h>
#include <string.h>
#include "platform/mbed_thread.h"
#include <BufferedSerial.h>
#include <rtos.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;
uint16_t ad_volt_buf;
uint8_t ad_flag;
char ad_dis[25];
uint16_t ad_dis_1[25];
}adc = {0,0,0,0,0,0};
// adc 객체선언
AnalogIn VR(PA_0, 3.3); // A0
AnalogIn VR_2(PA_1); //A1); // A0
// time out 객체선언
Timeout time_irq;
Timeout time_irq_5;
// Thread 객체선언
Thread my_thread_1;
Thread my_thread_2;
Thread my_thread_3;
Thread my_thread_4;
uint16_t cnt = 1234;
uint8_t digit_cnt = 0;
uint8_t ad_ch_flag = 0;
char t_flag = 0;
void my_time_3()
{
// 30ms
led1 = !led1;
/*
fnd_595_out(digit_cnt, cnt);
*/
}
void my_time_4()
{
led2 = !led2;
// 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;
}
*/
// timeout Interrupt
void t_irq()
{
led3 = !led3;
t_flag = 1;
// 다시 재 장전 = Timerout Set
// time_irq.attach(&t_irq, 0.5);
// digit_cnt++;
// if(digit_cnt > 4) digit_cnt = 1;
// fnd_595_out(digit_cnt, cnt);
}
void ad_func(uint8_t ch)
{
adc.ad_sum = adc.volt * 1000;
if(ch == 1)
{
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 if(ch == 2)
{
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 t_irq_5()
{
led4 = !led4;
time_irq_5.attach(&t_irq_5, 0.1);
}
void th1_func()
{
while(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);
rtx.write(adc.ad_dis, 22);
//HAL_Delay(50);
/*
led1 = !led1;
HAL_Delay(500);
rtx.write("thread-1\n", 9);
*/
}
}
void th2_func()
{
while(1)
{
led2 = !led2;
adc.volt = VR.read() * 3.3f; // 전압이 됨
adc.ad_sum = adc.volt * 1000;
adc. ad_volt_buf = 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);
rtx.write(adc.ad_dis, 22);
HAL_Delay(50);
// HAL_Delay(700);
// rtx.write("thread-2\n", 9);
}
}
void th3_func()
{
while(1)
{
led3 = !led3;
digit_cnt++;
if(digit_cnt > 4) digit_cnt = 1;
HAL_Delay(2);
rtx.write("thread-3\n", 9);
}
}
void th4_func()
{
while(1)
{
led4 = !led4;
HAL_Delay(5);
rtx.write("thread-4\n", 9);
}
}
int main() {
// put your setup code here, to run once:
//Tiket timer
my_tick_3.attach(&my_time_3, 0.2); // 30mssec
my_tick_4.attach(&my_time_4, 1.0); // 340mssec
my_tick_1.attach(&ad_ch_1, 0.8);
my_tick_2.attach(&ad_ch_2, 1.0);
// timeout
//time_irq.attach(&t_irq, 0.5);
//time_irq_5.attach(&t_irq_5, 0.1);
my_thread_1.start(th1_func);
my_thread_2.start(th2_func);
my_thread_3.start(th3_func);
my_thread_4.start(th4_func);
while(1)
{
fnd_595_out(digit_cnt, adc.ad_volt_buf);
rtx.write("prg-main\n", 9);
thread_sleep_for(10); // 500ms
/*
if(t_flag ==1)
{
// 다시 재 장전 = Timerout Set
// time_irq.attach(&t_irq, 0.5);
t_flag =0;
}
*/
// 실습2 Tiker timer + adc
//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);
*/
}
}