devreci
Aktif Üye
- Katılım
- 25 Mart 2020
- Mesajlar
- 477
smps power:
#include "stm8s.h"
/////////// by devreci 2-2014 https://www.youtube.com/@devreci
static const uint8_t dgt[]={ 0xee,0x24,0xd6,0xb6,0x3c,0xba,0xfa,0x26,0xfe,0xbe,0xf0,0xf8,0xd0,0xf4,0x1f,0xca,0x0,0x0 };
static const uint8_t dgtc[]={ 0xf2,0xb3,0xe3,0x73,0xd3,0xf1,0xf3 };
uint8_t syf[]={ 1,2,3,4,5,6};
uint8_t Ayr_v,Ayr_a,Ayrhg,Ayrypy,tHerm,fsnon,vltyok,AyrDsgt;
uint16_t Vtis,Ampis,Ampdgr,Ynsn,syy,fanpwm,fanpwmis;
uint16_t Amp,Vlt,Vlto,Ampo,Pwm;
const uint16_t _ssd=2760;
const uint8_t _sxv=5;
void ito(uint16_t sy,uint8_t bs)
{
bs-=2;
for(uint8_t j=0;j<3;j++) { syf[bs]=sy%10; sy/=10; bs++; }
bs--; if((syf[bs]==0)&&(bs<3)&&(sy<1)) syf[bs]=16;
}
void flshwrt();
INTERRUPT_HANDLER(EXTI_PrtAIRQHandler,3)
{
uint8_t ssd=GPIOA->IDR&14;
if(ssd==14) { Ayrhg^=1; Ynsn=15000; return; }
if(ssd==4) { Ayrypy=30; AyrDsgt=1; if(!Ayrhg) { Vtis+=Ayr_v; Ayr_v+=3; if(Vtis>1010) Vtis=1010; Vtis-=Vtis%3; Vtis+=2; } else { Ampis+=Ayr_a; Ayr_a+=3; if(Ampis>990) Ampis=990; } return; }
if(ssd==2) { Ayrypy=30; AyrDsgt=1; if(!Ayrhg) { Vtis-=Ayr_v; Ayr_v+=3; if(Vtis>1030) Vtis=0; Vtis-=Vtis%3; Vtis++; } else { Ampis-=Ayr_a; Ayr_a+=3; if(Ampis>1000) Ampis=0; } return; }
}
INTERRUPT_HANDLER(EXTI_TimIRQHandler,23)
{
__asm( "__buz: \r\n"
"ldw y,Vlto; \r\n"
"subw y,$53e2; \r\n"
"sraw y; \r\n"
"addw y,Pwm; \r\n"
"ldw Pwm,y; \r\n"
"ldw x,Ampdgr; \r\n"
"subw x,$53e0; \r\n"
"ldw Ampo,x; \r\n"
"ldw x,Vtis; \r\n"
"subw x,$53e2; \r\n"
"cpw x,Ampo; \r\n"
"jrsle __hgh; \r\n"
"ldw x,Ampo; \r\n"
"__hgh: \r\n"
"addw x,Pwm; \r\n"
"tnzw x;\r\n "
"jrpl __kj; \r\n"
"clrw x;\r\n"
"__kj: \r\n"
"cpw x,_ssd \r\n"
"jrule __sk \r\n"
"ldw x,_ssd \r\n"
"__sk:\r\n"
"ldw Pwm,x \r\n"
"ld a,4; \r\n"
"sraw x; \r\n"
"sraw x; \r\n"
"sraw x; \r\n"
"sraw x; \r\n"
"ld a,xl; \r\n"
"cp a,_sxv;\r\n"
"jruge __sux; \r\n"
"clr a; \r\n"
"__sux: \r\n"
"ld $5266,a;\r\n"
"ld $5268,a;\r\n"
"__sddf:\r\n"
"mov Vlto,$53e2; \r\n"
"mov Vlto+1,$53e3; \r\n"
);
TIM4->SR1=0;
}
void trrm() { if(tHerm==123) { syf[2]=16; if((syy&0x1fff)<0xa00) { syf[1]=14; syf[0]=15; } else { syf[1]=16; syf[0]=16; } syf[5]=16; syf[4]=16; syf[3]=16; } }
void waitt() { IWDG->KR=0xaa; __asm( " ld a,#200; \r\n__buz: \r\n nop; \r\n nop; \r\n dec a; \r\n jrne __buz \r\n" ); IWDG->KR=0xaa; }
void epromwrt(uint16_t out,uint16_t dgr) { out++; *((uint8_t*) out)=dgr&0xff; waitt(); out--; dgr/=256; *((uint8_t*) out)=dgr; waitt(); }
///Amaper offset
void ampofst() { if(Ampis<110) { Ampdgr=110-Ampis; Ampdgr/=11; } else { Ampdgr=8; } Ampdgr+=Ampis; if(Ampdgr>990) Ampdgr=990; }
void Basla()
{
Ayr_v=3; Ayr_a=0; Ayrhg=0; Ayrypy=0; tHerm=0; fsnon=0; AyrDsgt=0;
Ynsn=0; syy=0; fanpwm=0; fanpwmis=0; fanpwm=160;
Vtis=*((uint16_t*)0x4004); if(Vtis==1234) { Vtis=*((uint16_t*)0x4000); Ampis=*((uint16_t*)0x4002); } else { Vtis=151; Ampis=101; }
Amp=Ampis; Vlt=Vtis; ampofst(); Vlto=1; Pwm=0; vltyok=0;
IWDG->KR=0xAA;
sim();
CLK->CKDIVR=0;
GPIOB->DDR=0;
GPIOB->ODR=0XC0;
GPIOB->DDR=0XC0;
GPIOB->CR1=0xC0;
GPIOB->CR2=0xC0;
GPIOD->ODR=0Xff;
GPIOD->DDR=0Xff;
GPIOD->CR1=0xff;
GPIOD->CR2=0xff;
GPIOC->ODR=0Xf0;
GPIOC->DDR=0Xf0;
GPIOC->CR1=0xf0;
GPIOC->CR2=0xf0;
GPIOE->ODR=0Xf0;
GPIOE->DDR=0Xf0;
GPIOE->CR1=0xf0;
GPIOE->CR2=0xf0;
GPIOF->DDR=0X00; GPIOF->CR2=0x00; GPIOF->CR1=0x10;
TIM1->CR1=1;
TIM1->CR2=0;
TIM1->SR2=2;
TIM1->CCMR1=0x70;
TIM1->CCMR2=0x70;
TIM1->CCMR3=0x70;
TIM1->CCER1=0x13;
TIM1->CCER2=0x03;
TIM1->PSCRH=0; TIM1->PSCRL=0;
TIM1->ARRH=1; TIM1->ARRL=90;
TIM1->CCR1H=0; TIM1->CCR1L=0;
TIM1->CCR2H=0; TIM1->CCR2L=0;
TIM1->CCR3H=0; TIM1->CCR3L=0;
TIM1->BKR =0x80;
ADC1->CSR=3;
ADC1->CR1=0X43;
ADC1->CR2=0X0A;
ADC1->CR3=0X80;
ADC1->CR1|=1;
ADC1->CR1|=1;
IWDG->KR=0xAA;
GPIOA->DDR=0;
GPIOA->CR1=0x0e;
GPIOA->CR2=0x0e;
EXTI->CR1=0x01;
EXTI->CR2=0x2;
TIM4->CR1=1;
TIM4->IER=1;
TIM4->SR1=0;
TIM4->PSCR=4;
TIM4->ARR=60;
IWDG->KR=0xcc;
IWDG->KR=0x55;
IWDG->PR=4;
IWDG->RLR=0xff;
IWDG->KR=0xaa;
rim();
}
void flshwrt()
{
GPIOB->ODR=0X00;
GPIOD->ODR=0x00;
GPIOC->ODR=0X00;
GPIOE->ODR=0x00;
TIM4->CR1=0; IWDG->KR=0xaa;
TIM1->CR1=9;
TIM1->CCR1H=0; TIM1->CCR1L=0; TIM1->CCR2H=0; TIM1->CCR2L=0; TIM1->CCR3H=0; TIM1->CCR3L=0;
if(AyrDsgt) {
FLASH->DUKR=0xae;
FLASH->DUKR=0x56; IWDG->KR=0xaa;
while(!(FLASH->IAPSR&8));
waitt();
epromwrt(0x4000,Vtis); epromwrt(0x4002,Ampis); epromwrt(0x4004,1234);
FLASH->IAPSR=0;
FLASH->DUKR=0; AyrDsgt=0; }
uint16_t shh=0;
while(shh<60000){ IWDG->KR=0xaa; if(GPIOF->IDR&0x10) shh++; else shh=0; }
TIM4->CR1=1; TIM1->CR1=1;
}
uint8_t ddt=0;
int main( void )
{
Basla();
while(1)
{
IWDG->KR=0xaa;
ddt++; if(ddt>5) ddt=0;
uint8_t aa=dgtc[ddt];
if(Ynsn!=0) { Ynsn--; if(Ynsn&0x800) { if((Ynsn&0x3f)<45) { if(Ayrhg) { if(ddt>2) aa=0xff; } else { if(ddt<3) aa=0xff; } } } }
GPIOD->ODR=aa&1; GPIOC->ODR=aa&0xf0;
GPIOE->ODR=(aa&2)<<4;
GPIOD->ODR|=dgt[syf[ddt]];
if((ddt==1)||(ddt==5)) GPIOB->ODR|=0xC0; else GPIOB->ODR&=~0x80;
for(uint32_t i=0;i<14;i++);
GPIOD->ODR=0x1; GPIOC->ODR=0xff; GPIOE->ODR=0x20;
for(uint32_t i=0;i<10;i++);
syy++;
Amp*=15; Amp+=*((uint16_t*) &ADC1->DB0RH); Amp/=16;
if((syy&0xff)==0) {
Vlt*=3; Vlt+=*((uint16_t*) &ADC1->DB1RH); Vlt>>=2;
if(GPIOF->IDR&0x10) vltyok=0; else vltyok++;
if(vltyok>2) flshwrt();
if((syy&0x3ff)==0) {
if(Ayr_v!=3) { Ayr_v-=3; if(Ayr_v>100) Ayr_v=3; if(Ayr_v>40) Ayr_v=40; }
if(Ayr_a!=3) { Ayr_a-=3; if(Ayr_a>104) Ayr_a=1; if(Ayr_a>50) Ayr_a=50; } }
if(Ayrypy) Ayrypy--;
if((syy&0x7ff)==0) { if((Ayrhg)||(!Ayrypy)) ito(Vlt/3,2); if((syy&0xfff)==0) { if((!Ayrhg)||(!Ayrypy)) ito(Amp,5); } trrm(); }
if((syy&0xfff)==0) {
uint16_t pu=Amp/6;
if(pu<*((uint16_t*) &ADC1->DB2RH)) pu=*((uint16_t*) &ADC1->DB2RH); if(pu<*((uint16_t*) &ADC1->DB3RH)) { pu=*((uint16_t*) &ADC1->DB3RH); }
if(pu>650) { if(tHerm!=123) { TIM4->CR1=0; TIM1->CCR1H=0; TIM1->CCR1L=0; TIM1->CCR2H=0; TIM1->CCR2L=0; fanpwm=570; TIM1->CR1=9; fsnon=1; } tHerm=123; }
if(pu<450) { if(tHerm==123) { TIM4->CR1=1; tHerm=0; TIM1->CR1=1; } }
pu-=250; if(pu>1024) pu=0;
if((pu>60)&&(!fsnon)) { fanpwm=150; fsnon=1; } if(fanpwm<pu) { fanpwm+=2; } if(fanpwm>pu) { fanpwm-=2; }
if(fanpwm>1024) fanpwm=0; if(fanpwm<40) { TIM1->CCR3H=0; TIM1->CCR3L=0; fsnon=0; } else { TIM1->CCR3H=(fanpwm>>8); TIM1->CCR3L=(fanpwm&0xff); }
}
}
if(Ayrypy) { if((syy&0x3f)==0) { if(Ayrhg) ito(Ampis,5); else ito(Vtis/3,2); trrm(); ampofst(); } }
}
return 0;
}
Çıkış testi
Son düzenleme: