gestione ciclo
This commit is contained in:
@@ -13,6 +13,7 @@ void StopMot(TIM_HandleTypeDef *htim,uint32_t Channel);
|
||||
void StartMot(TIM_HandleTypeDef *htim,uint32_t Channel,uint16_t pwmval);
|
||||
void SetMotPwm(TIM_HandleTypeDef *htim,uint32_t Channel,uint16_t pwmval);
|
||||
void SetMot(uint8_t mot,uint8_t dir,uint16_t pwmval);
|
||||
void SetMotPerc(uint8_t mot,uint8_t dir,uint8_t perc);
|
||||
|
||||
#define timMot1 &htim2
|
||||
#define FWMot1 TIM_CHANNEL_1
|
||||
|
||||
@@ -84,7 +84,7 @@
|
||||
* (when HSE is used as system clock source, directly or through the PLL).
|
||||
*/
|
||||
#if !defined (HSE_VALUE)
|
||||
#define HSE_VALUE 8000000U /*!< Value of the External oscillator in Hz */
|
||||
#define HSE_VALUE 12000000U /*!< Value of the External oscillator in Hz */
|
||||
#endif /* HSE_VALUE */
|
||||
|
||||
#if !defined (HSE_STARTUP_TIMEOUT)
|
||||
|
||||
@@ -19,6 +19,7 @@ extern uint16_t ch5 ;
|
||||
extern uint16_t ch6 ;
|
||||
extern uint16_t ch7 ;
|
||||
extern uint16_t ch8 ;
|
||||
extern const uint8_t deftab[];
|
||||
|
||||
bool toHex(char c1,char c2,char c3,char c4,uint16_t* retval){
|
||||
if((c1>='0')&&(c1<='9')){
|
||||
@@ -185,8 +186,21 @@ void manageCDC(void){
|
||||
sprintf((char*)s,"\n?ed");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else if(rxbuf[1]=='x'){
|
||||
if(rxidx==2){
|
||||
for(i=0;i<32;i++){
|
||||
st=EEW_Write(i,deftab[i]);
|
||||
if (st == EE_OK)sprintf((char*)s,"\ndone 0x%02x=0x%04x",i,deftab[i]);
|
||||
else sprintf((char*)s,"\nee write error %d", st);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?ex");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?er|w|d");
|
||||
sprintf((char*)s,"\n?er|w|d|x");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
|
||||
@@ -10,6 +10,41 @@
|
||||
* [2..] Records...
|
||||
*/
|
||||
|
||||
const uint8_t deftab[32]={
|
||||
40, //m1pwmap
|
||||
60, //m1pwmch
|
||||
50, //m2pwmap
|
||||
50, //m2pwmch
|
||||
40, //m3pwmap
|
||||
40, //m3pwmch
|
||||
40, //m4pwmap
|
||||
40, //m4pwmch
|
||||
20, //m1rampstart
|
||||
20, //m2rampstart
|
||||
20, //m3rampstart
|
||||
20, //m4rampstart
|
||||
26, //t1ap
|
||||
2, //t2ap
|
||||
8, //t3ap
|
||||
40, //t4ap
|
||||
1, //twap
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
};
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint16_t VirtAddress;
|
||||
|
||||
@@ -31,11 +31,36 @@
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN PTD */
|
||||
|
||||
typedef enum{
|
||||
omchiuso,
|
||||
omapertura1,
|
||||
omapertura2,
|
||||
omapertura3,
|
||||
omapertura4,
|
||||
omapertura5,
|
||||
omapertura6,
|
||||
omstopapertura,
|
||||
omaperto,
|
||||
omchiusura1,
|
||||
omchiusura2,
|
||||
omchiusura3,
|
||||
omchiusura4,
|
||||
omchiusura5,
|
||||
omchiusura6,
|
||||
omstopchiusura,
|
||||
}omCiclo_st;
|
||||
typedef enum{
|
||||
bzoff,
|
||||
bzmoving,
|
||||
}stBuz_st;
|
||||
/* USER CODE END PTD */
|
||||
|
||||
/* Private define ------------------------------------------------------------*/
|
||||
/* USER CODE BEGIN PD */
|
||||
#define P1START 0x01
|
||||
#define P1STOP 0x02
|
||||
#define P2START 0x04
|
||||
#define P2STOP 0x08
|
||||
|
||||
/* USER CODE END PD */
|
||||
|
||||
@@ -63,6 +88,12 @@ extern uint16_t ch7 ;
|
||||
extern uint16_t ch8 ;
|
||||
|
||||
uint8_t pulsanti=0;
|
||||
volatile uint8_t rtP1=0;
|
||||
volatile uint8_t rtP2=0;
|
||||
|
||||
uint8_t stPulsanti=0;
|
||||
omCiclo_st stCiclo=omchiuso;
|
||||
volatile stBuz_st stBuz=bzoff;
|
||||
/* USER CODE END PV */
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
@@ -88,26 +119,115 @@ void HAL_SYSTICK_Callback(void){
|
||||
|
||||
if (++c10ms >= 10) { // 10 ms
|
||||
c10ms = 0;
|
||||
if(rtP1)rtP1--;
|
||||
if(rtP2)rtP2--;
|
||||
|
||||
//**** legge i tasti********************************************************************************
|
||||
inidx++;
|
||||
inidx&=0x03;
|
||||
if(HAL_GPIO_ReadPin(P1_GPIO_Port, P1_Pin)==GPIO_PIN_SET)inbuf[inidx]|=INP1;else inbuf[inidx]&=(~INP1);
|
||||
if(HAL_GPIO_ReadPin(P2_GPIO_Port, P2_Pin)==GPIO_PIN_SET)inbuf[inidx]|=INP2;else inbuf[inidx]&=(~INP2);
|
||||
if(HAL_GPIO_ReadPin(P1_GPIO_Port, P1_Pin)==GPIO_PIN_RESET)inbuf[inidx]|=INP1;else inbuf[inidx]&=(~INP1);
|
||||
if(HAL_GPIO_ReadPin(P2_GPIO_Port, P2_Pin)==GPIO_PIN_RESET)inbuf[inidx]|=INP2;else inbuf[inidx]&=(~INP2);
|
||||
pulsanti|=(inbuf[0]&inbuf[1]&inbuf[2]&inbuf[3]);
|
||||
pulsanti&=(inbuf[0]|inbuf[1]|inbuf[2]|inbuf[3]);
|
||||
//**** legge adc ***********************************************************************************
|
||||
readAdc();
|
||||
if (++c100ms >= 10) { // 10 ms
|
||||
|
||||
c100ms = 0; //flag_10ms = 1; // set a flag; do real work in main loop
|
||||
//**** gestione buzzer *****************************************************************************
|
||||
if(stBuz==bzmoving){
|
||||
if(c1s>=5)HAL_GPIO_WritePin(GPIOA, BUZ_Pin, GPIO_PIN_RESET);else HAL_GPIO_WritePin(GPIOA, BUZ_Pin, GPIO_PIN_SET);
|
||||
}else{//bzoff
|
||||
HAL_GPIO_WritePin(GPIOA, BUZ_Pin, GPIO_PIN_RESET);
|
||||
}
|
||||
//**************************************************************************************************
|
||||
if (++c1s >= 10) { // 10 ms
|
||||
c1s = 0;
|
||||
HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
|
||||
HAL_GPIO_TogglePin(LED2_GPIO_Port, LED2_Pin);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void managePulsanti(void){
|
||||
if(pulsanti&INP1){
|
||||
if(rtP1==0)stPulsanti|=P1START;
|
||||
}else{
|
||||
if(stPulsanti&P1START)stPulsanti&=(~P1START);
|
||||
else if(rtP1<=190){
|
||||
stPulsanti|=P1STOP;
|
||||
}
|
||||
rtP1=200;
|
||||
}
|
||||
if(pulsanti&INP2){
|
||||
if(rtP2==0)stPulsanti|=P2START;
|
||||
}else{
|
||||
if(stPulsanti&P2START)stPulsanti&=(~P2START);
|
||||
else if(rtP2<=190)stPulsanti|=P2STOP;
|
||||
rtP2=200;
|
||||
}
|
||||
}
|
||||
|
||||
void manageCiclo(void){
|
||||
switch(stCiclo){
|
||||
case omchiuso:
|
||||
if(stPulsanti&P1START){
|
||||
stBuz=bzmoving;
|
||||
stCiclo=omapertura1;
|
||||
}
|
||||
if(stPulsanti&P1STOP)stCiclo=omstopapertura;
|
||||
break;
|
||||
case omapertura1:
|
||||
if(stPulsanti&P1STOP)stCiclo=omstopapertura;
|
||||
break;
|
||||
case omapertura2:
|
||||
if(stPulsanti&P1STOP)stCiclo=omstopapertura;
|
||||
break;
|
||||
case omapertura3:
|
||||
if(stPulsanti&P1STOP)stCiclo=omstopapertura;
|
||||
break;
|
||||
case omapertura4:
|
||||
if(stPulsanti&P1STOP)stCiclo=omstopapertura;
|
||||
break;
|
||||
case omapertura5:
|
||||
if(stPulsanti&P1STOP)stCiclo=omstopapertura;
|
||||
break;
|
||||
case omapertura6:
|
||||
if(stPulsanti&P1STOP)stCiclo=omstopapertura;
|
||||
break;
|
||||
case omstopapertura:
|
||||
stBuz=bzoff;
|
||||
stCiclo=omchiuso;
|
||||
stPulsanti&=(~P1STOP);
|
||||
break;
|
||||
case omaperto:
|
||||
if(stPulsanti&P1STOP)stCiclo=omstopchiusura;
|
||||
break;
|
||||
case omchiusura1:
|
||||
if(stPulsanti&P1STOP)stCiclo=omstopchiusura;
|
||||
break;
|
||||
case omchiusura2:
|
||||
if(stPulsanti&P1STOP)stCiclo=omstopchiusura;
|
||||
break;
|
||||
case omchiusura3:
|
||||
if(stPulsanti&P1STOP)stCiclo=omstopchiusura;
|
||||
break;
|
||||
case omchiusura4:
|
||||
if(stPulsanti&P1STOP)stCiclo=omstopchiusura;
|
||||
break;
|
||||
case omchiusura5:
|
||||
if(stPulsanti&P1STOP)stCiclo=omstopchiusura;
|
||||
break;
|
||||
case omchiusura6:
|
||||
if(stPulsanti&P1STOP)stCiclo=omstopchiusura;
|
||||
break;
|
||||
case omstopchiusura:
|
||||
stBuz=bzoff;
|
||||
stCiclo=omaperto;
|
||||
stPulsanti&=(~P1STOP);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/**
|
||||
@@ -156,7 +276,7 @@ int main(void)
|
||||
StopMot(timMot4,FWMot4);
|
||||
StopMot(timMot4,BWMot4);
|
||||
//CDC_Transmit_FS((uint8_t*)"Start\r\n", 7);
|
||||
while (CDC_Transmit_FS((uint8_t*)"Start\r\n", 7) == USBD_BUSY);
|
||||
//while (CDC_Transmit_FS((uint8_t*)"Start\r\n", 7) == USBD_BUSY);
|
||||
if (EE_Init() != EE_OK){
|
||||
for(;;);//errore eeprom
|
||||
}
|
||||
@@ -167,6 +287,8 @@ int main(void)
|
||||
while (1){
|
||||
manageCDC();
|
||||
manageAdc();
|
||||
managePulsanti();
|
||||
manageCiclo();
|
||||
// while (CDC_Available()) {
|
||||
// int c = CDC_ReadByte();
|
||||
//if (c < 0) break;
|
||||
@@ -209,7 +331,7 @@ void SystemClock_Config(void)
|
||||
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
|
||||
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
|
||||
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
|
||||
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
|
||||
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6;
|
||||
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
|
||||
@@ -135,3 +135,110 @@ void SetMot(uint8_t mot,uint8_t dir,uint16_t pwmval){
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void SetMotPerc(uint8_t mot,uint8_t dir,uint8_t perc){
|
||||
uint16_t pwmval;
|
||||
uint32_t tempval;
|
||||
tempval=perc;
|
||||
tempval*=16384;
|
||||
tempval/=100;
|
||||
pwmval=(uint16_t)tempval;
|
||||
switch(mot){
|
||||
case 1:
|
||||
if(dir==FW){
|
||||
if(pwmval==0){
|
||||
StopMot(timMot1,FWMot1);
|
||||
HAL_GPIO_WritePin(INH1_GPIO_Port, INH1_Pin, GPIO_PIN_RESET);
|
||||
}else if(IsPwmRunning(timMot1, FWMot1)){
|
||||
SetMotPwm(timMot1,FWMot1,pwmval);
|
||||
}else{
|
||||
HAL_GPIO_WritePin(INH1_GPIO_Port, INH1_Pin, GPIO_PIN_SET);
|
||||
StartMot(timMot1,FWMot1,pwmval);
|
||||
}
|
||||
}else{
|
||||
if(pwmval==0){
|
||||
StopMot(timMot1,BWMot1);
|
||||
HAL_GPIO_WritePin(INH1_GPIO_Port, INH1_Pin, GPIO_PIN_RESET);
|
||||
}else if(IsPwmRunning(timMot1, BWMot1)){
|
||||
SetMotPwm(timMot1,BWMot1,pwmval);
|
||||
}else{
|
||||
HAL_GPIO_WritePin(INH1_GPIO_Port, INH1_Pin, GPIO_PIN_SET);
|
||||
StartMot(timMot1,BWMot1,pwmval);
|
||||
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
if(dir==FW){
|
||||
if(pwmval==0){
|
||||
StopMot(timMot2,FWMot2);
|
||||
HAL_GPIO_WritePin(INH2_GPIO_Port, INH2_Pin, GPIO_PIN_RESET);
|
||||
}else if(IsPwmRunning(timMot2, FWMot2)){
|
||||
SetMotPwm(timMot2,FWMot2,pwmval);
|
||||
}else{
|
||||
HAL_GPIO_WritePin(INH2_GPIO_Port, INH2_Pin, GPIO_PIN_SET);
|
||||
StartMot(timMot2,FWMot2,pwmval);
|
||||
}
|
||||
}else{
|
||||
if(pwmval==0){
|
||||
StopMot(timMot2,BWMot2);
|
||||
HAL_GPIO_WritePin(INH2_GPIO_Port, INH2_Pin, GPIO_PIN_RESET);
|
||||
}else if(IsPwmRunning(timMot2, BWMot2)){
|
||||
SetMotPwm(timMot2,BWMot2,pwmval);
|
||||
}else{
|
||||
HAL_GPIO_WritePin(INH2_GPIO_Port, INH2_Pin, GPIO_PIN_SET);
|
||||
StartMot(timMot2,BWMot2,pwmval);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
if(dir==FW){
|
||||
if(pwmval==0){
|
||||
StopMot(timMot3,FWMot3);
|
||||
HAL_GPIO_WritePin(INH3_GPIO_Port, INH3_Pin, GPIO_PIN_RESET);
|
||||
}else if(IsPwmRunning(timMot3, FWMot3)){
|
||||
SetMotPwm(timMot3,FWMot3,pwmval);
|
||||
}else{
|
||||
HAL_GPIO_WritePin(INH3_GPIO_Port, INH3_Pin, GPIO_PIN_SET);
|
||||
StartMot(timMot3,FWMot3,pwmval);
|
||||
}
|
||||
}else{
|
||||
if(pwmval==0){
|
||||
StopMot(timMot3,BWMot3);
|
||||
HAL_GPIO_WritePin(INH3_GPIO_Port, INH3_Pin, GPIO_PIN_RESET);
|
||||
}else if(IsPwmRunning(timMot3, BWMot3)){
|
||||
SetMotPwm(timMot3,BWMot3,pwmval);
|
||||
}else{
|
||||
HAL_GPIO_WritePin(INH3_GPIO_Port, INH3_Pin, GPIO_PIN_SET);
|
||||
StartMot(timMot3,BWMot3,pwmval);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 4:
|
||||
if(dir==FW){
|
||||
if(pwmval==0){
|
||||
StopMot(timMot4,FWMot4);
|
||||
HAL_GPIO_WritePin(INH4_GPIO_Port, INH4_Pin, GPIO_PIN_RESET);
|
||||
}else if(IsPwmRunning(timMot4, FWMot4)){
|
||||
SetMotPwm(timMot4,FWMot4,pwmval);
|
||||
}else{
|
||||
HAL_GPIO_WritePin(INH4_GPIO_Port, INH4_Pin, GPIO_PIN_SET);
|
||||
StartMot(timMot4,FWMot4,pwmval);
|
||||
}
|
||||
}else{
|
||||
if(pwmval==0){
|
||||
StopMot(timMot4,BWMot4);
|
||||
HAL_GPIO_WritePin(INH4_GPIO_Port, INH4_Pin, GPIO_PIN_RESET);
|
||||
}else if(IsPwmRunning(timMot4, BWMot4)){
|
||||
SetMotPwm(timMot4,BWMot4,pwmval);
|
||||
}else{
|
||||
HAL_GPIO_WritePin(INH4_GPIO_Port, INH4_Pin, GPIO_PIN_SET);
|
||||
StartMot(timMot4,BWMot4,pwmval);
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user