Boas,
Tenho o seguinte código
unsigned int toggle = 0; //used to keep the state of the LED
unsigned int count = 0; //used to keep count of how many interrupts were fired
boolean flag = false;
//Timer2 Overflow Interrupt Vector, called every 1ms
ISR(TIMER2_OVF_vect)
{
count++; //Increments the interrupt counter
// if(count > 999) //1 seg
if(count > 499) // 500ms
{
toggle = !toggle; //toggles the LED state
tx_trame(trame);
//Serial.println("\n\rDEBUG timer");
//Serial.println("\n\r");
count = 0; //Resets the interrupt counter
}
digitalWrite(13,toggle); //LED
TCNT2 = 130; //Reset Timer to 130 out of 255
TIFR2 = 0x00; //Timer2 INT Flag Reg: Clear Timer Overflow Flag
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup(void)
{
Serial.begin(19200);
pinMode(etx, OUTPUT);
pinMode(13,OUTPUT);
Serial.println("DEBUG ok ok ");
//Setup Timer2 to fire every 1ms
TCCR2B = 0x00; //Disbale Timer2 while we set it up
TCNT2 = 130; //Reset Timer Count to 130 out of 255
TIFR2 = 0x00; //Timer2 INT Flag Reg: Clear Timer Overflow Flag
TIMSK2 = 0x01; //Timer2 INT Reg: Timer2 Overflow Interrupt Enable
TCCR2A = 0x00; //Timer2 Control Reg A: Wave Gen Mode normal
TCCR2B = 0x05; //Timer2 Control Reg B: Timer Prescaler set to 128
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop()
{
// tx_trame(trame);
// delay(500);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void tx_trame (byte *data)
{
byte i =0;
byte checksum =0;
static unsigned long ii=0;
digitalWrite(etx, HIGH); // escreve MAX485
delay(10); //****** problema ******
//__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t"); //delay
Serial.write(0xF3);
Serial.write(0xF7);
//for (int i=0; i<length; i++)
for (i=0; i<13; i++)
{
Serial.write(data[i]);
checksum ^= trame[i];
}
Serial.write(checksum);
delay(10); //****** problema ******
//delayMicroseconds(100);
/* Serial.println("\n\rDebug 8");
for (ii=0; ii<10000; ii++)
{
__asm__("nop\n\t"); //delay
// __asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t"); //delay
}
Serial.println("\n\rDebug 9");*/
digitalWrite(etx, LOW);
}
Se chamar a função tx_trame() pela função loop() ... tudo funciona as mil maravilhas ....
Mas desde que a chamo pelo timer2 ... chapéu ... nada funciona
Estou a usar a interrupção do timer2 todos os 500ms porque estou a controlar uma carta de E/S que tem que receber todos os 500ms o que fazer se não ao fim de 800ms reseta
A questão é como posso substituir a função delay(10); da função tx_trame(); para que funcione dentro do timer2
PS: Não sei porque é que o post original foi apagado.
Cumprimentos
Zé tretas