main.c DC PowerSupply

/**
******************************************************************************
* @file main.c
* @author hoangnv.3i@ggmail.com
* @version V1.0
* @date 19/5/2018
* @brief example1
******************************************************************************
**/

//——————————————————————————
//——————————————————————————
#include “stm8s.h”
#include “main.h”
#include <stdio.h>
#include <string.h>
#include “lcd1602.h”

//——————————
uint8_t g_time_adc_update,
g_adc_index=1,
g_index_channel=0,
flag_adc_intterrup=0;

uint16_t g_adc_average[2]={0,0};

uint32_t g_vol = 0,g_ampe = 0;
uint8_t g_TimeReadADC=0;

uint8_t g_index_adc_update;
uint16_t g_adc_buffer[2];

uint16_t g_time_led;

//——————————
void main(void)
{
Clk_CPU_Init();
GPIO_Config();
ADC_Init();

lcd_init();
Varlue_Init();

IWDG_Init();
while (1)
{

IWDG_ReloadCounter();

Update_Data();
Progress_Control();
Display();

};

}

//——————————————————————

//——————————————————————
void Progress_Control(void)
{

g_time_led++;
if(g_time_led >= 100)
{
g_time_led = 0;
GPIO_WriteReverse(LED_CPU_GPIO,LED_CPU_PIN);
};
}
//——————————————————————
void GPIO_Config(void)
{
GPIO_Init(GPIOA, GPIO_PIN_3, GPIO_MODE_OUT_PP_LOW_FAST);

GPIO_Init(GPIOC, GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7, GPIO_MODE_OUT_PP_LOW_FAST);
GPIO_Init(GPIOD, GPIO_PIN_4|GPIO_PIN_5, GPIO_MODE_OUT_PP_LOW_FAST);

}
//——————————————————————

void Update_Data(void)
{
float temp;

Adc_read();

g_TimeReadADC++;
if(g_TimeReadADC >= 1)
{
g_TimeReadADC = 0;

if(g_index_adc_update > 59)
{
temp = g_adc_average[1];
temp = temp/g_index_adc_update; //lay trung binh/10lan
temp = (temp*VREF_CPU*10)/1024;
g_vol = temp*RATIO_VREF + VOL_OFFSET;
g_vol /= 10;

temp = g_adc_average[0];
temp = temp/g_index_adc_update; //lay trung binh/10lan
temp = (temp*VREF_CPU*10) / 1024;

if(temp > AMPE_OFFSET)
{
g_ampe = (temp/RATIO_AREF/RESISTOR_SHUNT) + AMPE_OFFSET;
}
else
{
g_ampe = temp/RATIO_AREF/RESISTOR_SHUNT;
};
g_ampe /= 10;

g_adc_average[0] = 0;
g_adc_average[1] = 0;
g_index_adc_update = 0;

}
else
{
g_index_adc_update++;

g_adc_average[0] += g_adc_buffer[0];
g_adc_average[1] += g_adc_buffer[1];
};

};

}

//——————————————————————————-
void Display(void)
{
uint8_t i, buff[16];
uint32_t power;

power = g_vol*g_ampe /100;

memset(buff,’ ‘,sizeof(buff));

buff[0] = (g_vol/1000) | 0x30;
buff[1] = (g_vol%1000)/100 | 0x30;
buff[2] = ‘.’;
buff[3] = ((g_vol%1000)%100)/10 | 0x30;
buff[4] = ((g_vol%1000)%100)%10 | 0x30;
buff[5] = ‘v’;

buff[10] = (g_ampe/1000) | 0x30;
buff[11] = (g_ampe%1000)/100 | 0x30;
buff[12] = ‘.’;
buff[13] = ((g_ampe%1000)%100)/10 | 0x30;
buff[14] = ((g_ampe%1000)%100)%10 | 0x30;
buff[15] = ‘a’;

lcd_gotoxy(0,0);
for(i=0;i<16;i++)
{
lcd_putchar(buff[i]);
};

//——————————-
memset(buff,’ ‘,sizeof(buff));
buff[0] = (power/1000) | 0x30;
buff[1] = (power%1000)/100 | 0x30;
buff[2] = ‘.’;
buff[3] = ((power%1000)%100)/10 | 0x30;
buff[4] = ((power%1000)%100)%10 | 0x30;
buff[5] = ‘w’;

lcd_gotoxy(1,0);
for(i=0;i<16;i++)
{
lcd_putchar(buff[i]);
};

}
//——————————————————————————-
void Varlue_Init(void)
{
uint8_t i;

for(i=0;i<4;i++)
{
lcd_gotoxy(0,0);
lcd_puts(“Suabientan247”);
Delay(1);
};

}

//——————————————————————————-
void Delay( uint16_t num)
{
uint32_t index = 0;

for(index = 100000; index != 0; index–)
{
};
}
//——————————————————————————-

void Clk_CPU_Init(void)
{
CLK_DeInit();
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); //16MHZ internal
CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1); //fMASTER = 16MHz
CLK_HSICmd(ENABLE); //enable osc internal
}

//——————————————————————————-
void IWDG_Init(void)
{

IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
IWDG_SetPrescaler(IWDG_Prescaler_256);
IWDG_SetReload(0xff); //1.02s
IWDG_Enable();

}
//——————————————————————————-

void Adc_Config_Channel(uint8_t adc_channel)
{
ADC1->CSR &= 0xF0; //clear all channel adc
ADC1->CSR |= adc_channel; //set channel adc convert
}
//——————————————————————————-
void Adc_read(void)
{
if( flag_adc_intterrup == 1 )
{
flag_adc_intterrup = 0;

switch(g_index_channel)
{
case 0: //channel 5
g_adc_buffer[0] = ADC1_GetConversionValue(); //read channel 5
Adc_Config_Channel(4); //set channel 4
break;
//————–
case 1: //channel 6
g_adc_buffer[1] = ADC1_GetConversionValue(); //read channel 6
Adc_Config_Channel(3); //set channel 3
break;
};

g_index_channel++;
if( g_index_channel > 1 )
{
g_index_channel = 0;
};

ADC1_StartConversion(); //start convert
};
}

//——————————————————————————-
void ADC_Init(void)
{

GPIO_Init(ADC_CHANNEL3_GPIO, ADC_CHANNEL3_PIN | ADC_CHANNEL4_PIN, GPIO_MODE_IN_FL_NO_IT);

//—————————-
ADC1_Init(ADC1_CONVERSIONMODE_SINGLE, //ADC1_CONVERSIONMODE_CONTINUOUS, ADC1_CONVERSIONMODE_SINGLE
ADC1_CHANNEL_3,
ADC1_PRESSEL_FCPU_D12,
ADC1_EXTTRIG_TIM,
DISABLE,
ADC1_ALIGN_RIGHT,
ADC1_SCHMITTTRIG_CHANNEL3 | ADC1_SCHMITTTRIG_CHANNEL4,
DISABLE);

ADC1_ITConfig(ADC1_IT_EOCIE,ENABLE);

enableInterrupts();
ADC1_StartConversion();
g_index_channel = 0;
}

//——————————————————————————

//——————————————–
INTERRUPT_HANDLER(ADC1_IRQHandler, 22)
{
flag_adc_intterrup = 1;
ADC1_ClearFlag(ADC1_FLAG_EOC);
}

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *