cfreund escreveu:Veja se entendi bem:
Você precisa identificar três situações de um pino.
1. Nivel Alto
2. Nivel Baixo.
3. Pulsando a 2 Hz com duty cycle de 50%.
É isso?
Aham, é isso Cfreund.
Moderadores: andre_luis, 51, guest2003, Renie
cfreund escreveu:Veja se entendi bem:
Você precisa identificar três situações de um pino.
1. Nivel Alto
2. Nivel Baixo.
3. Pulsando a 2 Hz com duty cycle de 50%.
É isso?
volatile uint8_t pulsando;
timer1_isr() {
static uint8_t evt_cnt = 0;
static uint8_t last, f = 0;
if (SINAL && last != SINAL) ++f; // incrementa f quando identificar uma mudança de 0 para 1 no pino SINAL.
last = SINAL;
if (++evt_cnt >= 5) { // 1 segundo
evt_cnt = 0;
if (f >= 2) pulsando = 1;
f = 0;
}
}
cfreund escreveu:Configure o timer1 para gerar interrupção a cada 200ms.
- Código: Selecionar todos
volatile uint8_t pulsando;
timer1_isr() {
static uint8_t evt_cnt = 0;
static uint8_t last, f = 0;
if (SINAL && last != SINAL) ++f; // incrementa f quando identificar uma mudança de 0 para 1 no pino SINAL.
last = SINAL;
if (++evt_cnt >= 5) { // 1 segundo
evt_cnt = 0;
if (f >= 2) pulsando = 1;
f = 0;
}
}
cfreund escreveu:'unsigned char'. Mas pode ser 'char'.
#define SINAL PIN_D0
#define LED PIN_C0
#define BUZZER PIN_C1
#define PULSO PIN_C2
static int conta=0;
static int contador;
volatile char pulsando;
#INT_TIMER1
void TIMER1_isr(void)
{
static char last, f = 0;
set_timer1(3036 + get_timer1()); //8 MHz
conta++;
if (SINAL && last != SINAL)
{
++f; // incrementa f quando identificar uma mudança de 0 para 1 no pino SINAL.
last = SINAL;
if (conta >= 5)
{ // 1 segundo
conta = 0;
}
if (f >= 2)
{
pulsando = 1;
f = 0;
}
}
}
void main()
{
int16 AD_A0,AD_A1,AD_A2;
int1 chamando = 0;
setup_adc_ports(sAN0|sAN1|sAN2); //ativei 3 portas analogicas A0,A1,A2
setup_adc(ADC_CLOCK_INTERNAL);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8); //262 ms overflow
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
while(TRUE)
{
set_adc_channel(0);
delay_ms(10);
AD_A0=read_adc();
set_adc_channel(1);
delay_ms(10);
AD_A1=read_adc();
set_adc_channel(2);
delay_ms(10);
AD_A2=read_adc();
/////////////////////////////////////////////////contador
if (conta == 4) // Se estourou 4 vezes = 1 segundo
{
seg++;
conta = 0;
}
///////////////////////////////////////////////GERADOR DE PULSO
if((conta % 2) == 0)
{
output_high(PULSO);
}
else
{
output_low(PULSO);
}
///////////////////////////////////////////////LEITURA DO SINAL
if(pulsando == 1)
{
chamando = 1;
//LIGA MEU SISTEMA
}
if(SINAL == TRUE) //parar chamada
{
chamando = 0;
output_low(LED);
output_low(BUZZER);
}
If(SINAL == FALSE)
{
// PROBLEMA NO SISTEMA
}
if(chamando == 1) //pulso
{
if((conta % 2) == 0)
{
output_high(LED);
output_high(BUZZER);
}
else
{
output_low(LED);
output_low(BUZZER);
}
}
} //while
} //void main
static int contador;
volatile int seg = 0;
volatile char pulsando;
#INT_TIMER1
void TIMER1_isr(void)
{
static char last, f = 0, conta = 0;
set_timer1(3036 + get_timer1()); //8 MHz
conta++;
if (SINAL && last != SINAL)
{
++f; // incrementa f quando identificar uma mudança de 0 para 1 no pino SINAL.
}
last = SINAL;
switch (++conta)
{
case 1:
output_high(PULSO); // Nivel alto durante 200 mS
break;
case 2:
output_low(PULSO); // baixo durante 800 mS
break;
case 5: // 1 segundo
conta = 0;
++seg;
if (f >= 2)
{
pulsando = 1;
}
f = 0;
break;
}
}
///////////////////////////////////////////////LEITURA DO SINAL
if(pulsando == 1)
{
pulsando = 0;
chamando = 1;
//LIGA MEU SISTEMA
}
/////////////////////////////////////////////////contador
if (conta == 4) // Se estourou 4 vezes = 1 segundo
{
seg++;
conta = 0;
}
///////////////////////////////////////////////GERADOR DE PULSO
if((conta % 2) == 0)
{
output_high(PULSO);
}
else
{
output_low(PULSO);
}
WHILE(TRUE)
{
p_conta = p_conta + 1;
if(input(BOTAO)
{
p_qtd_h = p_qtd_h + 1;
}
else
{
p_qtd_l = p_qtd_l +1;
}
if(p_conta == p_limite)
{
if(p_qtd_h == p_limite)
{
// CONDIÇÃO NORMAL (5V)
p_confirma = 0;
}
else
if(p_qtd_l == p_limite)
{
//CONDIÇÃO ERRO (0V)
p_confirma = 0;
}
else
{
p_confirma = p_confirma + 1;
if((p_confirma == 2) && (ultima_chamada == 1))
{
// CONDIÇÃO CHAMADA (PULSANTE)
p_confirma = 0;
ultima_chamada = 0;
}
}
p_conta = 0;
p_qtd_h = 0;
p_qtd_l = 0;
}
}
if(sinal) {
delay(xxms) // delay suficiente para que o sinal já tenha trocado de estado (já que voce conhece a frequencia e a mesma é fixa)
if(!sinal)
rotina_que_trata();
}
Usuários navegando neste fórum: Nenhum usuário registrado e 1 visitante