Você está na página 1de 6

#include <stdint.

h>
#include "inc/tm4c123gh6pm.h"
int display(int info1, int info2) {
int tabela[10] = { 0b00111111, 0b00000110, 0b01011011, 0b01001111,
0b01100110, 0b01101101, 0b01111101, 0b00000111, 0b01111111,
0b01101111 }; // lgica de converso
GPIO_PORTF_DATA_R |= (1 << 1);
// Ligar o primeiro display e mandar as informaes (unidades segundos)
if (GPIO_PORTF_DATA_R == (1 << 1)) {
GPIO_PORTF_DATA_R &= ~(1 << 1); // zera o clock deste display
GPIO_PORTF_DATA_R |= (1 << 2); // liga o clock do outro display
GPIO_PORTB_DATA_R = 0; // garante que inicialmente o display est apagado
for (int a = 0; a < 50; a++) {
}
GPIO_PORTB_DATA_R &= 0;
GPIO_PORTB_DATA_R |= tabela[info1]; // escreve a informao no display
// Delay
for (int a = 0; a < 1000; a++) {
}
}
// Ligar o segundo display e mandar as informaes (dezenas segundos)
if (GPIO_PORTF_DATA_R == (1 << 2)) {
GPIO_PORTF_DATA_R &= ~(1 << 2); // zera o clock deste display
GPIO_PORTF_DATA_R |= (1 << 1); // liga o clock do outro display
GPIO_PORTB_DATA_R = 0; // garante que inicialmente o display est apagado
GPIO_PORTB_DATA_R &= 0;
for (int a = 0; a < 50; a++) {
}
GPIO_PORTB_DATA_R = tabela[info2]; // escreve a informao no display
// Delay
for (int a = 0; a < 1000; a++) {
}
}
}
int lerteclado() {
int linha1 = 1;
int linha2 = 0;
int resp = 11;

if (linha1 == 1) {
if ((GPIO_PORTE_DATA_R & (1 << 0)) != 0) {
resp = 0;
}
if ((GPIO_PORTE_DATA_R & (1 << 1)) != 0) {
resp = 1;
}
if ((GPIO_PORTE_DATA_R & (1 << 2)) != 0) {
resp = 2;
}
linha1 = 0;
linha2 = 1;
GPIO_PORTE_DATA_R &= ~(1 << 3);
GPIO_PORTE_DATA_R |= (1 << 4);
}
if (linha2 == 1) {
if ((GPIO_PORTE_DATA_R & (1 << 0)) != 0) {
resp = 3;
}
if ((GPIO_PORTE_DATA_R & (1 << 1)) != 0) {
resp = 4;
}
if ((GPIO_PORTE_DATA_R & (1 << 2)) != 0) {
resp = 5;
}
linha2 = 0;
linha1 = 1;
GPIO_PORTE_DATA_R &= ~(1 << 4);
GPIO_PORTE_DATA_R |= (1 << 3);
}
return (resp);
}
static void clockconfig() {
SYSCTL_RCC2_R |= SYSCTL_RCC2_USERCC2;
SYSCTL_RCC2_R |= SYSCTL_RCC2_BYPASS2;
SYSCTL_RCC_R &= ~SYSCTL_RCC_USESYSDIV;
SYSCTL_RCC_R &= ~SYSCTL_RCC_XTAL_M;
SYSCTL_RCC_R |= SYSCTL_RCC_XTAL_16MHZ;
SYSCTL_RCC_R &= ~SYSCTL_RCC_OSCSRC_M;

SYSCTL_RCC_R |= SYSCTL_RCC_OSCSRC_MAIN;
SYSCTL_RCC2_R &= ~SYSCTL_RCC2_PWRDN2;
SYSCTL_RCC2_R |= SYSCTL_RCC2_DIV400;
SYSCTL_RCC_R |= SYSCTL_RCC_USESYSDIV;
SYSCTL_RCC2_R &= ~SYSCTL_RCC2_SYSDIV2_M;
SYSCTL_RCC2_R &= ~SYSCTL_RCC2_SYSDIV2LSB;
SYSCTL_RCC2_R |= (1 << 24);
while ((SYSCTL_RIS_R & SYSCTL_RIS_PLLLRIS) == 0) {
}
SYSCTL_RCC2_R &= ~SYSCTL_RCC2_BYPASS2;
}
static void time(int milissegundos) {
// at 200ms, vlido apenas para o clock de 80Mhz
NVIC_ST_CTRL_R = 0;
// escolha do periodo de contagem
NVIC_ST_RELOAD_R = (milissegundos * 80000) - 1;
// limpar o resgistrador (pode-se escrever qualquer valor)
NVIC_ST_CURRENT_R = 0;
// prioridade
NVIC_PRI3_R |= (1 << 29);
// habilita o systick, interrupo e escolhe o clock como fonte da contagem
NVIC_ST_CTRL_R |= (1 << 0) | (1 << 1) | (1 << 2);
}
int flag = 0;
static unsigned valoratual = 0;
void SysTick_handler() {
valoratual++;
if (valoratual == 5) {
flag = 1;
valoratual = 0;
}
}
void timer0_handler() {
TIMER0_ICR_R |= TIMER_ICR_TATOCINT;
if ((GPIO_PORTF_DATA_R & (1 << 1)) == 1) {
GPIO_PORTF_DATA_R &= ~(1 << 1);
} else {
GPIO_PORTF_DATA_R |= (1 << 1);
}

}
static void timer0_on() {
// usar o timer 0
SYSCTL_RCGCTIMER_R |= SYSCTL_RCGCTIMER_R0;
// garantir que est desabilitado
TIMER0_CTL_R &= ~(TIMER_CTL_TAEN);
// habilitar adc
TIMER0_CTL_R |= TIMER_CTL_TAOTE;
// limpar as configuraes
TIMER0_CFG_R = 0;
// colocar no modo peridico
TIMER0_TAMR_R |= 0x02;
// ciclos de clock entre interrupes
TIMER0_TAILR_R = 79999;
// mscaras de interrupo
//TIMER0_IMR_R |= TIMER_IMR_TATOIM;
// prioridade da interrupo
NVIC_PRI8_R |= (1 << 31);
// habilita interrupo 35
NVIC_EN0_R |= (1 << 19);
// habilitar
TIMER0_CTL_R |= TIMER_CTL_TAEN;
}
static void adc0_ss3_on() {
// habilita o ADC
SYSCTL_RCGCADC_R |= SYSCTL_RCGCADC_R0;
// Habilita as portas GPIO
SYSCTL_RCGCGPIO_R |= SYSCTL_RCGCGPIO_R4;
// Habilita e configura os pinos (sera usado PE1)
GPIO_PORTE_DIR_R &= 0b00;
GPIO_PORTE_AFSEL_R |= 0b10;
GPIO_PORTE_DEN_R &= 0b00;
GPIO_PORTE_AMSEL_R = 0b10;
// controle das amostras

// desabilitar amostrador
ADC0_ACTSS_R &= ~(1<<3);
// configurar trigger event
ADC0_EMUX_R |= ADC_EMUX_EM3_TIMER;
// pino de entrada
ADC0_SSMUX3_R = 0b10;
// bits de controle de amostra
ADC0_SSCTL3_R = 0b0110;
// mascara de interrupo
ADC0_IM_R = ADC_IM_MASK3;
// habilitar amostrador
ADC0_ACTSS_R |= (1<<3);
ADC0_ISC_R = (1<<3);
// habilitar interrupo 33
NVIC_EN0_R |= (1 << 17);
// flag desta interrupo ADC0_ISC_R = (1<<3);
}
int adc0ss3out = 0;
void ADC0_SS3_handler() {
TIMER0_ICR_R |= TIMER_ICR_TATOCINT;
ADC0_ISC_R = (1<<3);
adc0ss3out = ADC0_SSFIFO3_R;
}
int main(void) {
volatile uint32_t ui32Loop;
clockconfig();
// Habilita as GPIOs usadas
SYSCTL_RCGCGPIO_R = SYSCTL_RCGCGPIO_R1 | SYSCTL_RCGCGPIO_R5;
// Realiza uma leitura apenas para inserir alguns ciclos apos habilitar
//o periferico
ui32Loop = SYSCTL_RCGCGPIO_R;
// Habilita e configura os pinos
GPIO_PORTF_DIR_R = (1<<1)|(1<<2)|(1<<3);
GPIO_PORTF_DEN_R = (1<<0)|(1<<1)|(1<<2)|(1<<3)|(1<<4);
GPIO_PORTB_DIR_R = 0b11111111;
GPIO_PORTB_DEN_R = 0b11111111;

// variveis usadas pelo display


int info1 = 0;
int info2 = 0;
adc0_ss3_on();
timer0_on();
// Laco infinito
while (1) {
if (adc0ss3out < 1000) {
GPIO_PORTF_DATA_R |= (1<<2);
}
else if (adc0ss3out > 3000) {
GPIO_PORTF_DATA_R |= (1<<3);
}
else {
GPIO_PORTF_DATA_R &= ~(1<<2);
GPIO_PORTF_DATA_R &= ~(1<<3);
}
}
}

Você também pode gostar