Reformulada estrutura de firmware para uso de bibliotecas próprias.

parent 8378cb7f
//Bibliotecas
#include <avr/io.h> //inclui biblioteca de input/output do AVR
#include <avr/eeprom.h> //inclui biblioteca de EEPROM
#include <avr/interrupt.h> //inclui biblioteca de interrupt
#include <stdlib.h> //inclui biblioteca padrao
#include <string.h> //inclui biblioteca de strings
#include <math.h>
const byte buffSize = 40;
unsigned char inputBuffer[buffSize];
const char startMarker = '<';
const char endMarker = '>';
byte bytesRecvd = 0;
boolean readInProgress = false;
boolean newDataFromPC = false;
unsigned char rcvStatus = 0;
unsigned char prot;
unsigned char messageFromPC[buffSize] = {0};
unsigned char rcvProt[2][3];
unsigned int rcvParam[6][3];
void getDataFromPC() {
// receive data from PC and save it into inputBuffer
while(!Serial.available()){}
if(Serial.available() > 0) {
char x = Serial.read();
// the order of these IF clauses is significant
if (x == endMarker) {
readInProgress = false;
newDataFromPC = true;
inputBuffer[bytesRecvd] = 0;
strcpy(messageFromPC, inputBuffer); // copy it to messageFromPC
}
if(readInProgress) {
inputBuffer[bytesRecvd] = x;
bytesRecvd ++;
if (bytesRecvd == buffSize) {
bytesRecvd = buffSize - 1;
}
}
if (x == startMarker) {
bytesRecvd = 0;
readInProgress = true;
}
}
}
void saveAndReplyToPC() {
if (newDataFromPC) {
newDataFromPC = false;
rcvProt[0][prot] = messageFromPC[0];
rcvProt[1][prot] = messageFromPC[1];
rcvParam [0][prot]= ((messageFromPC[3]-1) & 255)<<8 | ((messageFromPC[2])&255);
rcvParam [1][prot]= ((messageFromPC[5]-1) & 255)<<8 | ((messageFromPC[4])&255);
rcvParam [2][prot]= ((messageFromPC[7]-1) & 255)<<8 | ((messageFromPC[6])&255);
rcvParam [3][prot]= ((messageFromPC[9]-1) & 255)<<8 | ((messageFromPC[8])&255);
rcvParam [4][prot]= ((messageFromPC[11]-1) & 255)<<8 | ((messageFromPC[10])&255);
rcvParam [5][prot]= ((messageFromPC[13]-1) & 255)<<8 | ((messageFromPC[12])&255);
Serial.print("<");
Serial.print("P");
Serial.print("R");
Serial.println(">");
}
}
void ReplyToPC() {
if (newDataFromPC) {
newDataFromPC = false;
Serial.print("<");
Serial.print("C");
Serial.print("R");
Serial.println(">");
}
}
//Função principal:
int main()
{
pinMode( 22 , OUTPUT ); //define saída 22 do Arduino para indicação com LED de protocolos ligados/desligados
digitalWrite( 22, LOW);
Serial.begin(9600); //inicia comunicação serial
Serial.println("<Arduino is ready>");
sei(); //reativa interrupts
while(1)
{
getDataFromPC();
if(messageFromPC[0]=='I' && messageFromPC[1]=='P')
{
digitalWrite(22, HIGH);
ReplyToPC();
}
else if(messageFromPC[0]=='A' && messageFromPC[1]=='P')
{
digitalWrite(22, LOW);
ReplyToPC();
}
else
{
//Recebe primeiro protocolo
prot = 0;
saveAndReplyToPC();
prot++;
//Recebe segundo protocolo...
}
}
return 0;
}
/*
Com_GUI.h - Library for communication between Arduino MEGA and the Neurostimulator GUI
*/
#include "Com_GUI.h"
void Com_GUI::SerialConfig()
{
Serial.begin( BAUDRATE, SERIAL_8N1);
Serial.println( readyMessage );
}
void Com_GUI::GetDataFromPC()
{
// receive data from PC and save it into inputBuffer
while(!Serial.available()){}
if(Serial.available() > 0)
{
char x = Serial.read();
// the order of these IF clauses is significant
if (x == endMarker)
{
readInProgress = false;
newDataFromPC = true;
inputBuffer[bytesRecvd] = 0;
strcpy(messageFromPC, inputBuffer); // copy it to messageFromPC
}
if(readInProgress)
{
inputBuffer[bytesRecvd] = x;
bytesRecvd ++;
if (bytesRecvd == buffSize) {
bytesRecvd = buffSize - 1;
}
}
if (x == startMarker)
{
bytesRecvd = 0;
readInProgress = true;
}
}
}
void Com_GUI::ReplyToPC( char firstChar, char secondChar)
{
//Reply "CR" to GUI after a protocol jump or stop
if (newDataFromPC)
{
newDataFromPC = false;
Serial.print(startMarker);
Serial.print(firstChar);
Serial.print(secondChar);
Serial.println(endMarker);
}
}
void Com_GUI::SaveProtocolData()
{
//Receive protocol type and its parameters and returns "PR"
if (newDataFromPC)
{
newDataFromPC = false;
rcvProt[0][prot] = messageFromPC[0];
rcvProt[1][prot] = messageFromPC[1];
rcvParam[0][prot]= ((messageFromPC[3]-1) & 255)<<8 | ((messageFromPC[2])&255);
rcvParam[1][prot]= ((messageFromPC[5]-1) & 255)<<8 | ((messageFromPC[4])&255);
rcvParam[2][prot]= ((messageFromPC[7]-1) & 255)<<8 | ((messageFromPC[6])&255);
rcvParam[3][prot]= ((messageFromPC[9]-1) & 255)<<8 | ((messageFromPC[8])&255);
rcvParam[4][prot]= ((messageFromPC[11]-1) & 255)<<8 | ((messageFromPC[10])&255);
rcvParam[5][prot]= ((messageFromPC[13]-1) & 255)<<8 | ((messageFromPC[12])&255);
ReplyToPC('P', 'R');
}
}
unsigned char Com_GUI::RecvCommandHndlr()
{
if (messageFromPC[0]=='H' && messageFromPC[1]=='B')
{
ReplyToPC('H', 'B');
return 0;
}
else if(messageFromPC[0]=='I' && messageFromPC[1]=='P')
{
digitalWrite(22, HIGH);
//StimulationProtocols.Stop();
ReplyToPC('I', 'R');
return 1;
}
else if(messageFromPC[0]=='A' && messageFromPC[1]=='P')
{
digitalWrite(22, LOW);
//StimulationProtocols.Jump();
ReplyToPC('A', 'R');
return 2;
}
else
{
//Recebe primeiro protocolo
prot = 0;
SaveProtocolData();
prot++;
//Recebe segundo protocolo...
}
}
/*
Com_GUI.h - Library for communication between Arduino MEGA and the Neurostimulator GUI
*/
#ifndef Com_GUI_h
#define Com_GUI_h
//Libraries
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <Stdint.h>
#include <Arduino.h>
#define BAUDRATE 9600
class Com_GUI
{
public:
void SerialConfig();
void GetDataFromPC();
void SaveProtocolData();
void ReplyToPC( char firstChar, char secondChar );
unsigned char RecvCommandHndlr();
//void HeartBeat();
//void ParseData();
uint8_t prot = 0;
static const byte buffSize = 40;
unsigned char messageFromPC[buffSize] = {0};
private:
const char *readyMessage = "<Arduino is ready>";
const char startMarker = '<';
const char endMarker = '>';
unsigned char inputBuffer[buffSize];
uint8_t bytesRecvd;
bool readInProgress = false;
bool newDataFromPC = false;
unsigned char rcvStatus = 0;
unsigned char rcvProt[2][3];
unsigned int rcvParam[6][3];
};
#endif
/*
StimulationProtocols.cpp - Library for use with Arduino MEGA
*/
//Timer 1 (12 (inv), 11 (n-inv), 13 (n-inv))
//Timer 3 (5 (inv), 2 (n-inv), 3 (n-inv))
//Timer 4 (7 (inv), 6 (n-inv), 8 (n-inv))
//Timer 5 (45 (inv), 46 (n-inv), 44 (n-inv))
#include "StimulationProtocols.h"
StimulationProtocols::StimulationProtocols()
{
pulseCounter = 0;
state = 0;
}
void StimulationProtocols::UnipolarBasalConfig( uint16_t H, uint16_t IPI, uint16_t ITI, bool PositivePulse )
{
//Limites testados:
//T entre 20us e 1000us
//IPI entre 1ms e 64ms
//ITI entre 1s e 60s
cli(); //desativa interrupts para fazer configuracao
//-----------------------------------------------------------CANAL 1 - TIMER 1
//Ativa pinos do canal 1:
if(PositivePulse)
DDRB |= (1 << PB6); //ativa pino PB6 (OC1B) - pino 12
else
DDRB |= (1 << PB5); //ativa pino PB5 (OC1A) - pino 11
DDRB |= (1 << PB7); //ativa pino PB7 (OC1C) - pino 13
//Limpa registradores de controle do timer 1
TCCR1A = 0;
TCCR1B = 0;
//Define timer 1 no modo Fast-PWM 14 (TOP definido por ICR1)
TCCR1A |= (1 << WGM11);
TCCR1B |= (1 << WGM12);
TCCR1B |= (1 << WGM13);
//Saida de PWM nao-inversor em A e inversor em B e nao-inversor em C
TCCR1A |= ((1 << COM1A1) | (1 << COM1B1) | (1 << COM1B0) | (1 << COM1C1));
TCCR1A &= ~(1 << COM1A0);
TCCR1A &= ~(1 << COM1C0);
// Define frequencia do PWM e Duty Cycle
ICR1 = ((IPI*1000 + H)/4-1);
OCR1A = ICR1/(((1000*IPI)+H)/H);
OCR1B = ICR1/(((1000*IPI)+H)/H);
OCR1C = ICR1/(((1000*IPI)+H)/H);
//Inicia timer 1
TCCR1B |= ((1 << CS11)| (1 << CS10)); //define prescaler de 64 para timer 1 e incia
//Ativa interrupt do timer 1:
TIMSK1 |= (1<<OCIE1A);
//-----------------------------------------------------------CANAL 2 - TIMER 3
//Ativa pinos do canal 3:
if(PositivePulse)
DDRE |= (1 << PE5); //ativa pino PE5 (OC3C) - pino 5
else
DDRE |= (1 << PE3); //ativa pino PE3 (OC3A) - pino 2
DDRE |= (1 << PE4); //ativa pino PE4 (OC3B) - pino 3
//Limpa registradores de controle do timer 3
TCCR3A = 0;
TCCR3B = 0;
//Define timer 3 no modo Fast-PWM 14 (TOP definido por ICR3)
TCCR3A |= (1 << WGM31);
TCCR3B |= (1 << WGM32);
TCCR3B |= (1 << WGM33);
//Saida de PWM nao-inversor em A e inversor em B e nao-inversor em C
TCCR3A |= ((1 << COM3A1) | (1 << COM3B1) | (1 << COM3B0) | (1 << COM3C1));
TCCR3A &= ~(1 << COM3A0);
TCCR3A &= ~(1 << COM3C0);
// Define frequencia do PWM e Duty Cycle
ICR3 = ((IPI*1000 + H)/4-1);
OCR3A = ICR3/(((1000*IPI)+H)/H);
OCR3B = ICR3/(((1000*IPI)+H)/H);
OCR3C = ICR3/(((1000*IPI)+H)/H);
//Inicia timer 3
TCCR3B |= ((1 << CS31)| (1 << CS30)); //define prescaler de 64 para timer 3 e inicia
//-----------------------------------------------------------CANAL 3 - TIMER 4
//Ativa pinos do canal 3:
if(PositivePulse)
DDRH |= (1 << PH4); //ativa pino PH4 (OC4B) - pino 7
else
DDRH |= (1 << PH3); //ativa pino PH3 (OC4A) - pino 6
DDRH |= (1 << PH5); //ativa pino PH5 (OC4C) - pino 8
//Limpa registradores de controle do timer 4
TCCR4A = 0;
TCCR4B = 0;
//Define timer 4 no modo Fast-PWM 14 (TOP definido por ICR4)
TCCR4A |= (1 << WGM41);
TCCR4B |= (1 << WGM42);
TCCR4B |= (1 << WGM43);
//Saida de PWM nao-inversor em A e inversor em B e nao-inversor em C
TCCR4A |= ((1 << COM4A1) | (1 << COM4B1) | (1 << COM4B0) | (1 << COM4C1));
TCCR4A &= ~(1 << COM4A0);
TCCR4A &= ~(1 << COM4C0);
// Define frequencia do PWM e Duty Cycle
ICR4 = ((IPI*1000 + H)/4-1);
OCR4A = ICR4/(((1000*IPI)+H)/H);
OCR4B = ICR4/(((1000*IPI)+H)/H);
OCR4C = ICR4/(((1000*IPI)+H)/H);
//Inicia timer 4
TCCR4B |= ((1 << CS41)| (1 << CS40)); //define prescaler de 64 para timer 4 e inicia
//-----------------------------------------------------------CANAL 4 - TIMER 4
//Ativa pinos do canal 4:
if(PositivePulse)
DDRL |= (1 << PL4); //ativa pino PL4 (OC5B) - pino 45
else
DDRL |= (1 << PL3); //ativa pino PL3 (OC5A) - pino 46
DDRL |= (1 << PL5); //ativa pino PL5 (OC5C) - pino 44
//Limpa registradores de controle do timer 5
TCCR5A = 0;
TCCR5B = 0;
//Define timer 5 no modo Fast-PWM 14 (TOP definido por ICR5)
TCCR5A |= (1 << WGM51);
TCCR5B |= (1 << WGM52);
TCCR5B |= (1 << WGM53);
//Saida de PWM nao-inversor em A e inversor em B e nao-inversor em C
TCCR5A |= ((1 << COM5A1) | (1 << COM5B1) | (1 << COM5B0) | (1 << COM5C1));
TCCR5A &= ~(1 << COM5A0);
TCCR5A &= ~(1 << COM5C0);
// Define frequencia do PWM e Duty Cycle
ICR5 = ((IPI*1000 + H)/4-1);
OCR5A = ICR5/(((1000*IPI)+H)/H);
OCR5B = ICR5/(((1000*IPI)+H)/H);
OCR5C = ICR5/(((1000*IPI)+H)/H);
//Inicia timer 5
TCCR5B |= ((1 << CS51)| (1 << CS50)); //define prescaler de 64 para timer 5 e inicia
sei(); //Reativa os interrupts
}
void StimulationProtocols::UnipolarBasalInterrupt( uint16_t H, uint16_t IPI, uint16_t ITI)
{
pulseCounter++; //incrementa contagem de pulso
if(pulseCounter==2 && state == 1) //concluídos 2 pulsos, desativa PWM
{
TCCR1A &= ~(1 << COM1A0);
TCCR1A &= ~(1 << COM1A1);
TCCR1A &= ~(1 << COM1B0);
TCCR1A &= ~(1 << COM1B1);
TCCR1A &= ~(1 << COM1C0);
TCCR1A &= ~(1 << COM1C1);
TCCR3A &= ~(1 << COM3A0);
TCCR3A &= ~(1 << COM3A1);
TCCR3A &= ~(1 << COM3B0);
TCCR3A &= ~(1 << COM3B1);
TCCR3A &= ~(1 << COM3C0);
TCCR3A &= ~(1 << COM3C1);
TCCR4A &= ~(1 << COM4A0);
TCCR4A &= ~(1 << COM4A1);
TCCR4A &= ~(1 << COM4B0);
TCCR4A &= ~(1 << COM4B1);
TCCR4A &= ~(1 << COM4C0);
TCCR4A &= ~(1 << COM4C1);
TCCR5A &= ~(1 << COM5A0);
TCCR5A &= ~(1 << COM5A1);
TCCR5A &= ~(1 << COM5B0);
TCCR5A &= ~(1 << COM5B1);
TCCR5A &= ~(1 << COM5C0);
TCCR5A &= ~(1 << COM5C1);
state = 0; //entra em estado de PWM desligado
pulseCounter=0; //zera contagem de pulsos
}
if(pulseCounter==((1000000*ITI+1000*IPI)/(H+1000*IPI)) && state == 0) //concluído intervalo de tempo desligado ITI, reativa PWM
{
TCCR1A |= ((1 << COM1A1) | (1 << COM1B1) | (1 << COM1B0) | (1 << COM1C1));
TCCR1A &= ~(1 << COM1A0);
TCCR1A &= ~(1 << COM1C0);
TCCR3A |= ((1 << COM3A1) | (1 << COM3B1) | (1 << COM3B0) | (1 << COM3C1));
TCCR3A &= ~(1 << COM3A0);
TCCR3A &= ~(1 << COM3C0);
TCCR4A |= ((1 << COM4A1) | (1 << COM4B1) | (1 << COM4B0) | (1 << COM4C1));
TCCR4A &= ~(1 << COM4A0);
TCCR4A &= ~(1 << COM4C0);
TCCR5A |= ((1 << COM5A1) | (1 << COM5B1) | (1 << COM5B0) | (1 << COM5C1));
TCCR5A &= ~(1 << COM5A0);
TCCR5A &= ~(1 << COM5C0);
pulseCounter = 0; //limpa quantidade de pulsos
state = 1; //entra em estado de PWM ligado
}
}
void StimulationProtocols::Stop()
{
DDRB |= (0 << PB5);
DDRB |= (0 << PB6);
DDRB |= (0 << PB7);
DDRE |= (0 << PE3);
DDRE |= (0 << PE4);
DDRE |= (0 << PE5);
DDRH |= (0 << PH3);
DDRH |= (0 << PH4);
DDRH |= (0 << PH5);
DDRL |= (0 << PL3);
DDRL |= (0 << PL4);
DDRL |= (0 << PL5);
}
/*
StimulationProtocols.h - Library for use with Arduino MEGA
*/
//Timer 1 (12 (inv), 11 (n-inv), 13 (n-inv))
//Timer 3 (5 (inv), 2 (n-inv), 3 (n-inv))
//Timer 4 (7 (inv), 6 (n-inv), 8 (n-inv))
//Timer 5 (45 (inv), 46 (n-inv), 44 (n-inv))
#ifndef StimulationProtocols_h
#define StimulationProtocols_h
//Libraries
#include <avr/io.h>
#include <avr/eeprom.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
class StimulationProtocols
{
public:
StimulationProtocols();
//void ProtocolOrder();
void UnipolarBasalConfig( uint16_t H, uint16_t IPI, uint16_t ITI, bool PositivePulse );
void UnipolarBasalInterrupt( uint16_t H, uint16_t IPI, uint16_t ITI );
void Stop();
//void Jump();
private:
uint16_t pulseCounter;
uint8_t state;
};
#endif
//Libraries
#include <avr/io.h> //inclui biblioteca de input/output do AVR
#include <avr/eeprom.h> //inclui biblioteca de EEPROM
#include <avr/interrupt.h> //inclui biblioteca de interrupt
#include <stdlib.h> //inclui biblioteca padrao
#include <string.h> //inclui biblioteca de strings
#include <math.h>
#include <Stdint.h>
#include "StimulationProtocols.h"
#include "Com_GUI.h"
//Main Function
int main()
{
Com_GUI Com_GUI;
pinMode( 22 , OUTPUT );
digitalWrite( 22, LOW);
Com_GUI.SerialConfig();
sei();
while(1)
{
Com_GUI.GetDataFromPC();
Com_GUI.RecvCommandHndlr();
}
return 0;
}
//Bibliotecas
#include <avr/io.h> //inclui biblioteca de input/output do AVR
#include <avr/eeprom.h> //inclui biblioteca de EEPROM
#include <avr/interrupt.h> //inclui biblioteca de interrupt
#include <stdlib.h> //inclui biblioteca padrao
#include <string.h> //inclui biblioteca de strings
#include <math.h>
//Variaveis globais
unsigned char protocol = 0;
unsigned int H = 200; //define largura de pulso (em us)
unsigned char IPI = 1; //define intervalo entre pulsos (em ms)
unsigned long int pulso=0;
unsigned char estado=1;
unsigned int ITI = 10; //define intervalo entre trens de pulso (em s)
unsigned int IEI = 200; //define intervalo entre epocas (em ms)
unsigned char n_pulso = 4;
unsigned char n_epoca = 10;
unsigned int epoca = 0;
unsigned int contador = 0;
//Rotinas de interrupcao
ISR(TIMER1_COMPA_vect) //interrupt quando acontece match entre OCR1X e TCNT1 (PWM baixa)
{
switch(protocol)
{
case 'b':
//PROTOCOLO BASAL UNIPOLAR
pulso++; //incrementa contagem de pulso
if(pulso==2 && estado == 1) //concluídos 2 pulsos, desativa PWM
{
TCCR1A &= ~(1 << COM1A0);
TCCR1A &= ~(1 << COM1A1);
TCCR1A &= ~(1 << COM1B0);
TCCR1A &= ~(1 << COM1B1);
TCCR1A &= ~(1 << COM1C0);
TCCR1A &= ~(1 << COM1C1);
TCCR3A &= ~(1 << COM3A0);
TCCR3A &= ~(1 << COM3A1);
TCCR3A &= ~(1 << COM3B0);
TCCR3A &= ~(1 << COM3B1);
TCCR3A &= ~(1 << COM3C0);
TCCR3A &= ~(1 << COM3C1);
TCCR4A &= ~(1 << COM4A0);
TCCR4A &= ~(1 << COM4A1);
TCCR4A &= ~(1 << COM4B0);
TCCR4A &= ~(1 << COM4B1);
TCCR4A &= ~(1 << COM4C0);
TCCR4A &= ~(1 << COM4C1);
TCCR5A &= ~(1 << COM5A0);
TCCR5A &= ~(1 << COM5A1);
TCCR5A &= ~(1 << COM5B0);
TCCR5A &= ~(1 << COM5B1);
TCCR5A &= ~(1 << COM5C0);
TCCR5A &= ~(1 << COM5C1);
estado = 0; //entra em estado de PWM desligado
pulso=0; //zera contagem de pulsos
}
if(pulso==((1000000*ITI+1000*IPI)/(H+1000*IPI)) && estado == 0) //concluído intervalo de tempo desligado ITI, reativa PWM
{
TCCR1A |= ((1 << COM1A1) | (1 << COM1B1) | (1 << COM1B0) | (1 << COM1C1));
TCCR1A &= ~(1 << COM1A0);
TCCR1A &= ~(1 << COM1C0);
TCCR3A |= ((1 << COM3A1) | (1 << COM3B1) | (1 << COM3B0) | (1 << COM3C1));
TCCR3A &= ~(1 << COM3A0);
TCCR3A &= ~(1 << COM3C0);
TCCR4A |= ((1 << COM4A1) | (1 << COM4B1) | (1 << COM4B0) | (1 << COM4C1));
TCCR4A &= ~(1 << COM4A0);
TCCR4A &= ~(1 << COM4C0);
TCCR5A |= ((1 << COM5A1) | (1 << COM5B1) | (1 << COM5B0) | (1 << COM5C1));
TCCR5A &= ~(1 << COM5A0);
TCCR5A &= ~(1 << COM5C0);
pulso = 0; //limpa quantidade de pulsos
estado = 1; //entra em estado de PWM ligado
}
break;
case 't':
//PROTOCOLO TBS UNIPOLAR
pulso++; //incrementa contagem de pulso
if(epoca==(n_epoca+1))
{
if(contador == ((ITI)/(IPI+H)))
{
epoca = 0;
estado = 1;
pulso = 0;
contador = 0;
}
else
{
contador++;
}
}
else
{
if(pulso==n_pulso && estado == 1) //se tem o terceiro pulso, desativa PWM
{
TCCR1A &= ~(1 << COM1A0);
TCCR1A &= ~(1 << COM1A1);
TCCR1A &= ~(1 << COM1B0);
TCCR1A &= ~(1 << COM1B1);
TCCR1A &= ~(1 << COM1C0);
TCCR1A &= ~(1 << COM1C1);
TCCR3A &= ~(1 << COM3A0);
TCCR3A &= ~(1 << COM3A1);
TCCR3A &= ~(1 << COM3B0);
TCCR3A &= ~(1 << COM3B1);
TCCR3A &= ~(1 << COM3C0);
TCCR3A &= ~(1 << COM3C1);
TCCR4A &= ~(1 << COM4A0);
TCCR4A &= ~(1 << COM4A1);
TCCR4A &= ~(1 << COM4B0);
TCCR4A &= ~(1 << COM4B1);
TCCR4A &= ~(1 << COM4C0);
TCCR4A &= ~(1 << COM4C1);
TCCR5A &= ~(1 << COM5A0);
TCCR5A &= ~(1 << COM5A1);
TCCR5A &= ~(1 << COM5B0);
TCCR5A &= ~(1 << COM5B1);
TCCR5A &= ~(1 << COM5C0);
TCCR5A &= ~(1 << COM5C1);
estado = 0; //entra em estado de PWM desligado
pulso=0;
epoca++; //incrementa contagem de epoca
}
if(pulso==((IPI+IEI)/(IPI+H)) && estado == 0)
{
TCCR1A |= ((1 << COM1A1) | (1 << COM1B1) | (1 << COM1B0) | (1 << COM1C1));
TCCR1A &= ~(1 << COM1A0);
TCCR1A &= ~(1 << COM1C0);
TCCR3A |= ((1 << COM3A1) | (1 << COM3B1) | (1 << COM3B0) | (1 << COM3C1));
TCCR3A &= ~(1 << COM3A0);
TCCR3A &= ~(1 << COM3C0);
TCCR4A |= ((1 << COM4A1) | (1 << COM4B1) | (1 << COM4B0) | (1 << COM4C1));
TCCR4A &= ~(1 << COM4A0);
TCCR4A &= ~(1 << COM4C0);
TCCR5A |= ((1 << COM5A1) | (1 << COM5B1) | (1 << COM5B0) | (1 << COM5C1));
TCCR5A &= ~(1 << COM5A0);
TCCR5A &= ~(1 << COM5C0);
pulso = 0; //limpa quantidade de pulsos
estado = 1; //entra em estado de PWM ligado
}
}
}
}
//Funções de configuração de protocolo:
void config_desativado() //configura desativação de protocolos
{
DDRB |= (0 << PB5); //ativa pino PB5 (OC1A) - pino 11
DDRB |= (0 << PB6); //ativa pino PB6 (OC1B) - pino 12
DDRB |= (0 << PB7); //ativa pino PB7 (OC1C) - pino 13
DDRE |= (0 << PE3); //ativa pino PE3 (OC3A) - pino 2
DDRE |= (0 << PE4); //ativa pino PE4 (OC3B) - pino 3
DDRE |= (0 << PE5); //ativa pino PE5 (OC3C) - pino 5
DDRH |= (0 << PH3); //ativa pino PH3 (OC4A) - pino 6
DDRH |= (0 << PH4); //ativa pino PH4 (OC4B) - pino 7
DDRH |= (0 << PH5); //ativa pino PH5 (OC4C) - pino 8
DDRL |= (0 << PL3); //ativa pino PL3 (OC5A) - pino 46
DDRL |= (0 << PL4); //ativa pino PL4 (OC5B) - pino 45
DDRL |= (0 << PL5); //ativa pino PL5 (OC5C) - pino 44
}
void config_basal_unipolar() //configura protocolo basal unipolar
{
//--------------------------------------------TIMER 1:
cli(); //desativa interrupts para fazer configuracao
//PWM
//Ativa pinos do canal 1: