Skip to content

Instantly share code, notes, and snippets.

@enrichman
Last active June 12, 2016 12:29
Show Gist options
  • Save enrichman/64b41be9dceb7156e8759e4f75364b69 to your computer and use it in GitHub Desktop.
Save enrichman/64b41be9dceb7156e8759e4f75364b69 to your computer and use it in GitHub Desktop.
Esercizio Rapporto
/****************************************************************************
* $Id:: tmrtest.c 6098 2011-01-08 02:26:20Z nxp12832 $
* Project: NXP LPC17xx Timer example
*
* Description:
* This file contains Timer test modules, main entry, to test Timer APIs.
*
****************************************************************************
* Software that is described herein is for illustrative purposes only
* which provides customers with programming information regarding the
* products. This software is supplied "AS IS" without any warranties.
* NXP Semiconductors assumes no responsibility or liability for the
* use of the software, conveys no license or title under any patent,
* copyright, or mask work right to the product. NXP Semiconductors
* reserves the right to make changes in the software without
* notification. NXP Semiconductors also make no representation or
* warranty that such application will be suitable for the specified
* use without further testing or modification.
****************************************************************************/
#include <cr_section_macros.h>
#include <NXP/crp.h>
// Variable to store CRP value in. Will be placed automatically
// by the linker when "Enable Code Read Protect" selected.
// See crp.h header for more information
__CRP const unsigned int CRP_WORD = CRP_NO_CRP ;
#include "LPC17xx.h"
#include "type.h"
#include "timer.h"
#include "HD44780.h"
#include "ADC.h"
#define STATE_0 0;
#define STATE_DISPLAY_S1 1;
#define STATE_DISPLAY_S2 2;
#define STATE_DISPLAY_RAPPORTO 3;
//Global variables definition
uint8_t current_state = STATE_0;
uint8_t key_pressed_event = 0;
uint32_t s1 = 0;
uint32_t s2 = 0;
/* Routine di gesione dell'interrupt esterno
* EINT3 è in coumune con l'interrupt generato dalla GPIO.
* Gli if consentono di individiare la causa dell'interruzione */
void EINT3_IRQHandler(void)
{
if(LPC_GPIOINT->IO0IntStatF & (1<<9)) /* è stato rilevato un fronte negativo su P0.9 */
{
key_pressed_event = 1; //segnalamento pressione tasto
delayMs(30); // preve pausa antirimbalzo
LPC_GPIOINT->IO0IntClr = (1<<9);
}
}
/* STATI: */
void state_0()
{
//Here write output for current state
PutCommand(RETURN_HOME); delayMs(5);
WriteString((unsigned char*)"State_0 ", 0);
if(key_pressed_event) //if input, change state
{
uint32_t adcData;
uint16_t i; // counter
uint8_t n = 6; /* 2^n è il numero di campioni
acquisiti su cui calcolare la media */
// leggo i valori di S1, S2
for( i=0; i < (1<<n) ; i++)
{
adcData += ADC_Read(4);
}
adcData >>= n;
s1 = adcData;
for( i=0; i < (1<<n) ; i++)
{
adcData += ADC_Read(5);
}
adcData >>= n;
s2 = adcData;
// Next state
current_state = STATE_DISPLAY_S1;
key_pressed_event = 0; // event acquired -> reset it
}
}
void state_display_s1()
{
PutCommand(RETURN_HOME); delayMs(5);
WriteString((unsigned char*)"S1: ", 0);
Write_ndigitsval(s1, 4);
// Next state
current_state = STATE_DISPLAY_S2;
delayMs(1000);
PutCommand(DISPLAY_CLEAR); delayMs(5);
}
void state_display_s2()
{
PutCommand(RETURN_HOME); delayMs(5);
WriteString((unsigned char*)"S2: ", 0);
Write_ndigitsval(s2, 4);
// Next state
current_state = STATE_DISPLAY_RAPPORTO;
delayMs(1000);
PutCommand(DISPLAY_CLEAR); delayMs(5);
}
void state_display_rapporto()
{
PutCommand(RETURN_HOME); delayMs(5);
WriteString((unsigned char*)"S1/S2: ", 0);
uint32_t rappCent = (100 * s1) / s2;
Write_ndigitsval(rappCent / 100, 4);
WriteChar('.');
Write_ndigitsval0(rappCent % 100, 2);
// Next state
current_state = STATE_0;
delayMs(1000);
PutCommand(DISPLAY_CLEAR); delayMs(5);
}
/*
* Tabella dei possibili stati:
* la tabella punta alle funzioni in elenco (stati)
*/
void(*state_table[])(void) = { state_0, state_display_s1, state_display_s2, state_display_rapporto };
/*****************************************************************************
** Main Function main()
*****************************************************************************/
int main (void)
{
/* SystemClockUpdate() updates the SystemFrequency variable */
SystemClockUpdate();
//Enable interrupt
LPC_GPIOINT->IO0IntEnF = (1<<9);
NVIC_EnableIRQ(EINT3_IRQn);
ADC_Init(4); //inizializza ADC canale 4 (P1.30)
ADC_Init(5); //inizializza ADC canale 5 (P1.31)
InitLCD(); //inizializza LCD
PutCommand(DISPLAY_CLEAR); delayMs(5);
PutCommand(RETURN_HOME); delayMs(5);
WriteString((unsigned char*)"Inizio..", 0);
delayMs(2000);
/* Loop forever */
while(1)
{ //job start here
state_table[current_state]();
delayMs(10);
} //job end
//se arriva qui c'è stato un crash!!
return 0;
}
/*****************************************************************************
** End Of File
******************************************************************************/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment