Re: Acertar velocímetro com alteração de pneumáticos!
Esta aqui um suposto codigo, mas nao analisei (e o pouco que sei é de arduino)...
Quem puder e souber que de uma vista de olhos [
]
EDIT: este codigo é para este micro: http://search.digikey.com/us/en/prod...CPJ-ND/1791371
Esta aqui um suposto codigo, mas nao analisei (e o pouco que sei é de arduino)...
Code:
Code:
#include <hidef.h> // for EnableInterrupts macro
#include "derivative.h" // include peripheral declarations
//static variables
static unsigned int Scount; //counter for calculating speed
static int Rcounta; //RPM pulse counter
static byte lowflag; //1 if the speed signal is below 15Hz
static byte IRQflag; //indicator to calculate RPM
static float adjust; //speed frequency adjustment
//temp variables
float temp; //float number for speed adjustment calculation
unsigned int speed; //data to load into PWM registers
byte tempdata; //temporary storage byte
void main(void) {
EnableInterrupts;
//Clock setup
ICSC1 = 0b10111000; //Ext Ref; RDIV=128; IREFS = 0
ICSC2 = 0b10110100; //BDIV=1; Hi Range, Hi Gain, Oscillator
//System Options setup
SOPT1_RSTPE = 0; //PTA5 (pin 1) functions as IRQ pin
SOPT1_BKGDPE = 1; //PTA4 (pin 2) functions as Background pin
//
SOPT2_T1CH0PS = 0; //TPM Ch0 on PTA0 (pin 20)
//IRQ setup
IRQSC_IRQPDD = 1; //disable internal pullup
IRQSC_IRQEDG = 1; //rising edge sensitive
IRQSC_IRQPE = 1; //IRQ pin enabled
IRQSC_IRQMOD = 0; //edge detect only
//Port A setup
PTADD_PTADD2 = 0; //interrupt input PTA2
//Port B setup
PTBDD = 0x10; //interrupt input PTB0; dip switch inputs PTB1-3; PTB4 = LED output
PTBPE = 0x0F; //enable pullups on PTB0-3
//Port C setup
PTCDD = 0x00; //dip switch inputs PTC0-3
PTCPE = 0x0F; //pullups enabled for PTC0-3
//Port A pin 2 interrupt setup
PTASC_PTAMOD = 0; //edge detect mode
PTAPS_PTAPS2 = 1; //PTA2 interrupt enabled
PTAES_PTAES2 = 1; //rising edge detection
//Port B pin 0 interrupt setup
PTBSC_PTBMOD = 0; //edge detect mode
PTBPS_PTBPS0 = 1; //PTB0 interrupt enabled
PTBES_PTBES0 = 1; //rising edge detection
//PWM CH0 setup
TPM1SC_TOIE = 0; //disable timer overflow interrupts
TPM1SC_CPWMS = 0; //output compare mode
TPM1SC_CLKSA = 1; //clock source = bus clock
TPM1SC_CLKSB = 0;
TPM1SC_PS0 = 1; //prescalar of 2 (1MHz output resolution)
TPM1SC_PS1 = 0;
TPM1SC_PS2 = 0;
TPM1C0SC_CH0IE = 0; //disables interrupts on CH0
TPM1C0SC_MS0A = 1; //output compare mode
TPM1C0SC_MS0B = 0;
TPM1C0SC_ELS0A = 1; //toggle output on compare
TPM1C0SC_ELS0B = 0;
/*adjust value change - LED lights to indicate change has been made
value is 0.5% multiplied by 7-bit binary value on switches 1-7
if SW8 is ON then decrease speed reading, OFF increase speed reading
adjust value increases to decrease speed and vice versa*/
tempdata = (~PTCD & 0x07)<<3; //PTC0-2 form upper byte
tempdata += (~PTBD & 0x0F)>>1; //PTB1-3 form lower byte
if (PTCD_PTCD3){ //if sw8 OFF
adjust = 1+(tempdata*0.005); //decrease speed
}
else{ //if sw8 ON
adjust = 1-(tempdata*0.005); //increase speed
}
Rcounta = 0; //counter starts at zero
PTASC_PTAIE = 1; //Port A pin 2 interrupt request enabled
IRQSC_IRQIE = 1; //IRQ interrupt enabled
/************************************************** *******************************/
/*for loop - repeats forever*/
/************************************************** *******************************/
for(;;) {
__RESET_WATCHDOG();
Scount = 0;
lowflag = 0;
PTBSC_PTBACK = 1; //clear flag
while(!PTBSC_PTBIF); //wait for new pulse
PTBSC_PTBACK = 1; //clear flag
while(!PTBSC_PTBIF){ //count during this pulse
Scount++;
if (Scount > 8390){ // = 65535/7.875 (about a 15Hz speed signal)
lowflag = 1;
}//if
}//while
PTBSC_PTBACK = 1; //clear flag
//PWM modulus setting
if (lowflag == 0){
speed = Scount;
}
else{
speed = 8320;
}
temp = (float)speed;
temp *= 7.875;
temp *= adjust;
speed = (int)temp;
TPM1MODH = (speed & 0xFF00)>>8; //high byte
TPM1MODL = (speed & 0X00FF); //low byte
/*6th gear indicator LED
check RPM count once every
512 pulses of the speedo signal*/
if (IRQflag ==1){
if (Rcounta < 64){
PTBD_PTBD4 = 0; //LED on
}//if
else{
PTBD_PTBD4 = 1; //LED off
}//else
IRQflag = 0; //reset flag
Rcounta = 0; //reset RPM counts
IRQSC_IRQIE = 1; //re-enable Speed interrupt
PTASC_PTAIE = 1; //re-enable RPM interrupt
}//if
}//for loop - repeats forever
}//main
/* IRQ_ISR - ISR that accompanies the IRQ pin Interrupt
triggered by the speed signal divided by 256 input*/
interrupt 2 void IRQ_ISR(void) {
IRQSC_IRQACK = 1; //clear flag
IRQflag = 1; //set indicator
IRQSC_IRQIE = 0; //disable Speed interrupt
PTASC_PTAIE = 0; //disable RPM interrupt
}
/* PTA_ISR - ISR that accompanies the Port A pin 2 Interrupt
triggered by RPM signal*/
interrupt 20 void PTA_ISR(void) {
PTASC_PTAACK = 1; // clear flag
Rcounta++; //increment RPM count
}
]EDIT: este codigo é para este micro: http://search.digikey.com/us/en/prod...CPJ-ND/1791371
]
]
Comment