- Messaggi: 583
- Ringraziamenti ricevuti 1
cerco aiuto per inserire un pulsante nel codice
13 Anni 6 Mesi fa #1
da Guido
cerco aiuto per inserire un pulsante nel codice è stato creato da Guido
Ciao, per tanto che provi non riesco ad inserire un pulsante che mi faccia uscire la frequenza (onda) quando premo un pulsante, (ho rimane sempre inserita l'onda, oppure non esce niente), mi potete aiutare inserendo un pulsante nel codice perche possa capire dove sbaglo.
Grazie.
#include <p18f4550.h>
#pragma config FOSC = HS
#pragma config WDT = OFF
#pragma config LVP = OFF
#pragma config PBADEN = OFF
//OSC = HS Set to work for high speed
//WDT = OFF watchdog timer is off
//LVP = OFF LVP programming is off
//PBADEN = OFF Analog input on PORTB are off
//************************************************************
// Functions Prototypes
//************************************************************
void set_duty_cycle (int duty_cycle);
void turn_on_PWM1 (void);
void High_Int_Event (void);
//************************************************************
// EASY USB Related (just constants for the I/O)
//************************************************************
#define LED1 LATDbits.LATD0
#define LED2 LATDbits.LATD1
#define LED3 LATDbits.LATD2
#define LED4 LATDbits.LATD3
#define ENABLE_SELF_PW LATDbits.LATD7
#define PW_IN_EX_ENABLE LATDbits.LATD6
#define VCC_EX_ENABLE LATDbits.LATD5
#define USB_EX_ENABLE LATDbits.LATD4
#define S2 PORTBbits.RB4
#define S3 PORTBbits.RB5
#define USB_PW_SENS PORTAbits.RA1
#define SELF_PW_SENS PORTAbits.RA2
#define LED_ON 1
#define LED_OFF 0
#define ENABLE_SELF_PW_ON 0
#define ENABLE_SELF_PW_OFF 1
#define PW_IN_EX_ENABLE_ON 1
#define PW_IN_EX_ENABLE_OFF 0
#define VCC_EX_ENABLE_ON 0
#define VCC_EX_ENABLE_OFF 1
#define USB_EX_ENABLE_ON 0
#define USB_EX_ENABLE_OFF 1
//************************************************************
// Global variables
//************************************************************
//Samples
int sine[] = {250,299,346,389,427,458,481,495,500,495,481,458,427,389,346,299,250,201,154,111,73,42,19,5,0,5,19,42,73,111,154,201};
// Repeat the sample for x time
// sampling frequency must be changed to keep analog signal frequency costnat
const unsigned char REPEATING_FACTOR = 3;
//************************************************************
// Interrupt Handler
//************************************************************
#pragma code high_vector = 0x08
void high_interrupt (void) {
// go to the interrupt handlerconst
_asm GOTO High_Int_Event _endasm
}
#pragma code
#pragma interrupt High_Int_Event
void High_Int_Event (void) {
static unsigned char sample = 0;
static unsigned char repeat = 0;
if (PIR1bits.TMR2IF == 1) {
// I create a pulse on RC0 to be measured with the scope.
// Used just for debugging the sampling frequency
LATCbits.LATC0 = 1;
LATCbits.LATC0 = 0;
set_duty_cycle (sine[sample]);
repeat++;
if (repeat == REPEATING_FACTOR) {
repeat = 0;
sample++;
if (sample > 31) {
sample = 0;
}
}
}
//Clear Timer 2 Interrupt Flag
PIR1bits.TMR2IF = 0;
}
//************************************************************
// Main program
//************************************************************
void main (void){
// PORTA settings
LATA = 0x00;
TRISA = 0xFF;
// PORTB settings
LATB = 0x00;
TRISB = 0xFF;
// PORTC settings
LATC = 0x00;
TRISC = 0b11111010;
// PORTD settings
LATD = 0x00;
TRISD = 0x00;
// PORTE settings
LATE = 0x00;
TRISE = 0xFF;
//*************************************
// EasyUSB Initialization
//*************************************
ENABLE_SELF_PW = ENABLE_SELF_PW_ON;
PW_IN_EX_ENABLE = PW_IN_EX_ENABLE_OFF;
VCC_EX_ENABLE = VCC_EX_ENABLE_OFF;
USB_EX_ENABLE = USB_EX_ENABLE_OFF;
//*************************************
// Modules Initializaion
//*************************************
// Set the PWM frequency to 25152
PR2 = 198;
set_duty_cycle (250);
// Turn ON TMR2 and set Prescaler to 0
T2CON = 0x04;
turn_on_PWM1 ();
// Enable Timer 2 Interrupt
PIE1bits.TMR2IE = 1;
// Standrd interrupt (by default is already 0)
RCONbits.IPEN = 0;
// Enable global interrupt
INTCONbits.GIE = 1;
// Enable Peripheral Interrupt
INTCONbits.PEIE = 1 ;
// just loop for nothing...
while (1){
}
}
//************************************
// Set the Diuty Cicle
//************************************
void set_duty_cycle (int duty_cycle) {
CCPR1L = (unsigned char) (duty_cycle >> 2);
if (duty_cycle & 0x0001)
CCP1CONbits.DC1B0 = 0x0001;
else
CCP1CONbits.DC1B0 = 0x0000;
if (duty_cycle & 0x0002)
CCP1CONbits.DC1B1 = 0x0001;
else
CCP1CONbits.DC1B1 = 0x0000;
}
//************************************
// Turn ON the PWM module
//************************************
void turn_on_PWM1 (void) {
CCP1CON = CCP1CON | 0b00001100;
}
Grazie.
#include <p18f4550.h>
#pragma config FOSC = HS
#pragma config WDT = OFF
#pragma config LVP = OFF
#pragma config PBADEN = OFF
//OSC = HS Set to work for high speed
//WDT = OFF watchdog timer is off
//LVP = OFF LVP programming is off
//PBADEN = OFF Analog input on PORTB are off
//************************************************************
// Functions Prototypes
//************************************************************
void set_duty_cycle (int duty_cycle);
void turn_on_PWM1 (void);
void High_Int_Event (void);
//************************************************************
// EASY USB Related (just constants for the I/O)
//************************************************************
#define LED1 LATDbits.LATD0
#define LED2 LATDbits.LATD1
#define LED3 LATDbits.LATD2
#define LED4 LATDbits.LATD3
#define ENABLE_SELF_PW LATDbits.LATD7
#define PW_IN_EX_ENABLE LATDbits.LATD6
#define VCC_EX_ENABLE LATDbits.LATD5
#define USB_EX_ENABLE LATDbits.LATD4
#define S2 PORTBbits.RB4
#define S3 PORTBbits.RB5
#define USB_PW_SENS PORTAbits.RA1
#define SELF_PW_SENS PORTAbits.RA2
#define LED_ON 1
#define LED_OFF 0
#define ENABLE_SELF_PW_ON 0
#define ENABLE_SELF_PW_OFF 1
#define PW_IN_EX_ENABLE_ON 1
#define PW_IN_EX_ENABLE_OFF 0
#define VCC_EX_ENABLE_ON 0
#define VCC_EX_ENABLE_OFF 1
#define USB_EX_ENABLE_ON 0
#define USB_EX_ENABLE_OFF 1
//************************************************************
// Global variables
//************************************************************
//Samples
int sine[] = {250,299,346,389,427,458,481,495,500,495,481,458,427,389,346,299,250,201,154,111,73,42,19,5,0,5,19,42,73,111,154,201};
// Repeat the sample for x time
// sampling frequency must be changed to keep analog signal frequency costnat
const unsigned char REPEATING_FACTOR = 3;
//************************************************************
// Interrupt Handler
//************************************************************
#pragma code high_vector = 0x08
void high_interrupt (void) {
// go to the interrupt handlerconst
_asm GOTO High_Int_Event _endasm
}
#pragma code
#pragma interrupt High_Int_Event
void High_Int_Event (void) {
static unsigned char sample = 0;
static unsigned char repeat = 0;
if (PIR1bits.TMR2IF == 1) {
// I create a pulse on RC0 to be measured with the scope.
// Used just for debugging the sampling frequency
LATCbits.LATC0 = 1;
LATCbits.LATC0 = 0;
set_duty_cycle (sine[sample]);
repeat++;
if (repeat == REPEATING_FACTOR) {
repeat = 0;
sample++;
if (sample > 31) {
sample = 0;
}
}
}
//Clear Timer 2 Interrupt Flag
PIR1bits.TMR2IF = 0;
}
//************************************************************
// Main program
//************************************************************
void main (void){
// PORTA settings
LATA = 0x00;
TRISA = 0xFF;
// PORTB settings
LATB = 0x00;
TRISB = 0xFF;
// PORTC settings
LATC = 0x00;
TRISC = 0b11111010;
// PORTD settings
LATD = 0x00;
TRISD = 0x00;
// PORTE settings
LATE = 0x00;
TRISE = 0xFF;
//*************************************
// EasyUSB Initialization
//*************************************
ENABLE_SELF_PW = ENABLE_SELF_PW_ON;
PW_IN_EX_ENABLE = PW_IN_EX_ENABLE_OFF;
VCC_EX_ENABLE = VCC_EX_ENABLE_OFF;
USB_EX_ENABLE = USB_EX_ENABLE_OFF;
//*************************************
// Modules Initializaion
//*************************************
// Set the PWM frequency to 25152
PR2 = 198;
set_duty_cycle (250);
// Turn ON TMR2 and set Prescaler to 0
T2CON = 0x04;
turn_on_PWM1 ();
// Enable Timer 2 Interrupt
PIE1bits.TMR2IE = 1;
// Standrd interrupt (by default is already 0)
RCONbits.IPEN = 0;
// Enable global interrupt
INTCONbits.GIE = 1;
// Enable Peripheral Interrupt
INTCONbits.PEIE = 1 ;
// just loop for nothing...
while (1){
}
}
//************************************
// Set the Diuty Cicle
//************************************
void set_duty_cycle (int duty_cycle) {
CCPR1L = (unsigned char) (duty_cycle >> 2);
if (duty_cycle & 0x0001)
CCP1CONbits.DC1B0 = 0x0001;
else
CCP1CONbits.DC1B0 = 0x0000;
if (duty_cycle & 0x0002)
CCP1CONbits.DC1B1 = 0x0001;
else
CCP1CONbits.DC1B1 = 0x0000;
}
//************************************
// Turn ON the PWM module
//************************************
void turn_on_PWM1 (void) {
CCP1CON = CCP1CON | 0b00001100;
}
Si prega Accedi o Crea un account a partecipare alla conversazione.
- Guido
- Autore della discussione
- Platinum Member
Riduci
Di più
13 Anni 6 Mesi fa - 13 Anni 6 Mesi fa #2
da Mauro Laurenti
Risposta da Mauro Laurenti al topic Re: cerco aiuto per inserire un pulsante nel codice
Ciao Guido,
volendo disattivare la nota puoi intervenire sia sul duty cycle che sulle interruzioni.
Per esempio nel ciclo infinito while potresti disattivare le interruzioni se il pulsante non è premuto e attivarle solo se il pulsante è premuto.
il bit per abilitare disabilitare le interruzioni è :
INTCONbits.GIE = 1;
altrimenti potresti impostare il duty cycle su 0 qualora il pulsnate non dovesse essere premuto. Questo lo dovresti fare all'interno della routine delle interruzioni. Dunque dove viene scritto:
set_duty_cycle (sine[sample]);
metti un controllo sul pulsante.
se è premuto scrivi
set_duty_cycle (sine[sample]);
altrimenti:
set_duty_cycle (0);
per gestire più pulsanti è bene che associ una funzione per ogni pulsante (che potresti leggere sequenzialmente nel ciclo while) e all'interno della funzione associata ad ogni pulsante imposti anche il registro PR2 oltre che abilitare le interruzione o fare altro.
Poi potrai leggere i pulsanti via interrupt, magari usando dei PCF8574 come descritto nell'ultima Brief Note che trovi sul sito.
Saluti,
Mauro
volendo disattivare la nota puoi intervenire sia sul duty cycle che sulle interruzioni.
Per esempio nel ciclo infinito while potresti disattivare le interruzioni se il pulsante non è premuto e attivarle solo se il pulsante è premuto.
il bit per abilitare disabilitare le interruzioni è :
INTCONbits.GIE = 1;
altrimenti potresti impostare il duty cycle su 0 qualora il pulsnate non dovesse essere premuto. Questo lo dovresti fare all'interno della routine delle interruzioni. Dunque dove viene scritto:
set_duty_cycle (sine[sample]);
metti un controllo sul pulsante.
se è premuto scrivi
set_duty_cycle (sine[sample]);
altrimenti:
set_duty_cycle (0);
per gestire più pulsanti è bene che associ una funzione per ogni pulsante (che potresti leggere sequenzialmente nel ciclo while) e all'interno della funzione associata ad ogni pulsante imposti anche il registro PR2 oltre che abilitare le interruzione o fare altro.
Poi potrai leggere i pulsanti via interrupt, magari usando dei PCF8574 come descritto nell'ultima Brief Note che trovi sul sito.
Saluti,
Mauro
Ultima Modifica 13 Anni 6 Mesi fa da Mauro Laurenti.
Si prega Accedi o Crea un account a partecipare alla conversazione.
13 Anni 6 Mesi fa #3
da Guido
Risposta da Guido al topic Re: cerco aiuto per inserire un pulsante nel codice
Ci provo, grazie Mauro.
Si prega Accedi o Crea un account a partecipare alla conversazione.
- Guido
- Autore della discussione
- Platinum Member
Riduci
Di più
- Messaggi: 583
- Ringraziamenti ricevuti 1
13 Anni 6 Mesi fa #4
da Guido
Risposta da Guido al topic Re: cerco aiuto per inserire un pulsante nel codice
Ciao, ho fatto un primo tentativo per attivare la frequenza alla pressione del pulsante, putroppo non lo vede è sempre attiva.
Scrivo il codice modificato nel Interrupt Handler:
#include <p18f4550.h>
#pragma config FOSC = HS
#pragma config WDT = OFF
#pragma config LVP = OFF
#pragma config PBADEN = OFF
//OSC = HS Set to work for high speed
//WDT = OFF watchdog timer is off
//LVP = OFF LVP programming is off
//PBADEN = OFF Analog input on PORTB are off
//************************************************************
// Functions Prototypes
//************************************************************
void set_duty_cycle (int duty_cycle);
void turn_on_PWM1 (void);
void High_Int_Event (void);
//************************************************************
// EASY USB Related (just constants for the I/O)
//************************************************************
#define LED1 LATDbits.LATD0
#define LED2 LATDbits.LATD1
#define LED3 LATDbits.LATD2
#define LED4 LATDbits.LATD3
#define ENABLE_SELF_PW LATDbits.LATD7
#define PW_IN_EX_ENABLE LATDbits.LATD6
#define VCC_EX_ENABLE LATDbits.LATD5
#define USB_EX_ENABLE LATDbits.LATD4
#define S2 PORTBbits.RB4
#define S3 PORTBbits.RB5
#define USB_PW_SENS PORTAbits.RA1
#define SELF_PW_SENS PORTAbits.RA2
#define LED_ON 1
#define LED_OFF 0
#define ENABLE_SELF_PW_ON 0
#define ENABLE_SELF_PW_OFF 1
#define PW_IN_EX_ENABLE_ON 1
#define PW_IN_EX_ENABLE_OFF 0
#define VCC_EX_ENABLE_ON 0
#define VCC_EX_ENABLE_OFF 1
#define USB_EX_ENABLE_ON 0
#define USB_EX_ENABLE_OFF 1
//************************************************************
// Global variables
//************************************************************
//Samples
int sine[] = {250,299,346,389,427,458,481,495,500,495,481,458,427,389,346,299,250,201,154,111,73,42,19,5,0,5,19,42,73,111,154,201};
// Repeat the sample for x time
// sampling frequency must be changed to keep analog signal frequency costnat
const unsigned char REPEATING_FACTOR = 3;
//************************************************************
// Interrupt Handler
//************************************************************
#pragma code high_vector = 0x08
void high_interrupt (void) {
// go to the interrupt handlerconst
_asm GOTO High_Int_Event _endasm
}
#pragma code
#pragma interrupt High_Int_Event
void High_Int_Event (void) {
static unsigned char sample = 0;
static unsigned char repeat = 0;
if (PIR1bits.TMR2IF == 1) {
// I create a pulse on RC0 to be measured with the scope.
// Used just for debugging the sampling frequency
LATCbits.LATC0 = 1;
LATCbits.LATC0 = 0;
if (PORTBbits.RB4 == 0) {
set_duty_cycle (sine[sample]);
repeat++;
}
else {
set_duty_cycle (0);
}
if (repeat == REPEATING_FACTOR) {
repeat = 0;
sample++;
if (sample > 31) {
sample = 0;
}
}
}
//Clear Timer 2 Interrupt Flag
PIR1bits.TMR2IF = 0;
}
//************************************************************
// Main program
//************************************************************
void main (void){
// PORTA settings
LATA = 0x00;
TRISA = 0xFF;
// PORTB settings
LATB = 0x00;
TRISB = 0xFF;
// PORTC settings
LATC = 0x00;
TRISC = 0b11111010;
// PORTD settings
LATD = 0x00;
TRISD = 0x00;
// PORTE settings
LATE = 0x00;
TRISE = 0xFF;
//*************************************
// EasyUSB Initialization
//*************************************
ENABLE_SELF_PW = ENABLE_SELF_PW_ON;
PW_IN_EX_ENABLE = PW_IN_EX_ENABLE_OFF;
VCC_EX_ENABLE = VCC_EX_ENABLE_OFF;
USB_EX_ENABLE = USB_EX_ENABLE_OFF;
//*************************************
// Modules Initializaion
//*************************************
// Set the PWM frequency to 25152
PR2 = 198;
set_duty_cycle (250);
// Turn ON TMR2 and set Prescaler to 0
T2CON = 0x04;
turn_on_PWM1 ();
// Enable Timer 2 Interrupt
PIE1bits.TMR2IE = 1;
// Standrd interrupt (by default is already 0)
RCONbits.IPEN = 0;
// Enable global interrupt
INTCONbits.GIE = 1;
// Enable Peripheral Interrupt
INTCONbits.PEIE = 1 ;
// just loop for nothing...
while (1){
}
}
//************************************
// Set the Diuty Cicle
//************************************
void set_duty_cycle (int duty_cycle) {
CCPR1L = (unsigned char) (duty_cycle >> 2);
if (duty_cycle & 0x0001)
CCP1CONbits.DC1B0 = 0x0001;
else
CCP1CONbits.DC1B0 = 0x0000;
if (duty_cycle & 0x0002)
CCP1CONbits.DC1B1 = 0x0001;
else
CCP1CONbits.DC1B1 = 0x0000;
}
//************************************
// Turn ON the PWM module
//************************************
void turn_on_PWM1 (void) {
CCP1CON = CCP1CON | 0b00001100;
}
Scrivo il codice modificato nel Interrupt Handler:
#include <p18f4550.h>
#pragma config FOSC = HS
#pragma config WDT = OFF
#pragma config LVP = OFF
#pragma config PBADEN = OFF
//OSC = HS Set to work for high speed
//WDT = OFF watchdog timer is off
//LVP = OFF LVP programming is off
//PBADEN = OFF Analog input on PORTB are off
//************************************************************
// Functions Prototypes
//************************************************************
void set_duty_cycle (int duty_cycle);
void turn_on_PWM1 (void);
void High_Int_Event (void);
//************************************************************
// EASY USB Related (just constants for the I/O)
//************************************************************
#define LED1 LATDbits.LATD0
#define LED2 LATDbits.LATD1
#define LED3 LATDbits.LATD2
#define LED4 LATDbits.LATD3
#define ENABLE_SELF_PW LATDbits.LATD7
#define PW_IN_EX_ENABLE LATDbits.LATD6
#define VCC_EX_ENABLE LATDbits.LATD5
#define USB_EX_ENABLE LATDbits.LATD4
#define S2 PORTBbits.RB4
#define S3 PORTBbits.RB5
#define USB_PW_SENS PORTAbits.RA1
#define SELF_PW_SENS PORTAbits.RA2
#define LED_ON 1
#define LED_OFF 0
#define ENABLE_SELF_PW_ON 0
#define ENABLE_SELF_PW_OFF 1
#define PW_IN_EX_ENABLE_ON 1
#define PW_IN_EX_ENABLE_OFF 0
#define VCC_EX_ENABLE_ON 0
#define VCC_EX_ENABLE_OFF 1
#define USB_EX_ENABLE_ON 0
#define USB_EX_ENABLE_OFF 1
//************************************************************
// Global variables
//************************************************************
//Samples
int sine[] = {250,299,346,389,427,458,481,495,500,495,481,458,427,389,346,299,250,201,154,111,73,42,19,5,0,5,19,42,73,111,154,201};
// Repeat the sample for x time
// sampling frequency must be changed to keep analog signal frequency costnat
const unsigned char REPEATING_FACTOR = 3;
//************************************************************
// Interrupt Handler
//************************************************************
#pragma code high_vector = 0x08
void high_interrupt (void) {
// go to the interrupt handlerconst
_asm GOTO High_Int_Event _endasm
}
#pragma code
#pragma interrupt High_Int_Event
void High_Int_Event (void) {
static unsigned char sample = 0;
static unsigned char repeat = 0;
if (PIR1bits.TMR2IF == 1) {
// I create a pulse on RC0 to be measured with the scope.
// Used just for debugging the sampling frequency
LATCbits.LATC0 = 1;
LATCbits.LATC0 = 0;
if (PORTBbits.RB4 == 0) {
set_duty_cycle (sine[sample]);
repeat++;
}
else {
set_duty_cycle (0);
}
if (repeat == REPEATING_FACTOR) {
repeat = 0;
sample++;
if (sample > 31) {
sample = 0;
}
}
}
//Clear Timer 2 Interrupt Flag
PIR1bits.TMR2IF = 0;
}
//************************************************************
// Main program
//************************************************************
void main (void){
// PORTA settings
LATA = 0x00;
TRISA = 0xFF;
// PORTB settings
LATB = 0x00;
TRISB = 0xFF;
// PORTC settings
LATC = 0x00;
TRISC = 0b11111010;
// PORTD settings
LATD = 0x00;
TRISD = 0x00;
// PORTE settings
LATE = 0x00;
TRISE = 0xFF;
//*************************************
// EasyUSB Initialization
//*************************************
ENABLE_SELF_PW = ENABLE_SELF_PW_ON;
PW_IN_EX_ENABLE = PW_IN_EX_ENABLE_OFF;
VCC_EX_ENABLE = VCC_EX_ENABLE_OFF;
USB_EX_ENABLE = USB_EX_ENABLE_OFF;
//*************************************
// Modules Initializaion
//*************************************
// Set the PWM frequency to 25152
PR2 = 198;
set_duty_cycle (250);
// Turn ON TMR2 and set Prescaler to 0
T2CON = 0x04;
turn_on_PWM1 ();
// Enable Timer 2 Interrupt
PIE1bits.TMR2IE = 1;
// Standrd interrupt (by default is already 0)
RCONbits.IPEN = 0;
// Enable global interrupt
INTCONbits.GIE = 1;
// Enable Peripheral Interrupt
INTCONbits.PEIE = 1 ;
// just loop for nothing...
while (1){
}
}
//************************************
// Set the Diuty Cicle
//************************************
void set_duty_cycle (int duty_cycle) {
CCPR1L = (unsigned char) (duty_cycle >> 2);
if (duty_cycle & 0x0001)
CCP1CONbits.DC1B0 = 0x0001;
else
CCP1CONbits.DC1B0 = 0x0000;
if (duty_cycle & 0x0002)
CCP1CONbits.DC1B1 = 0x0001;
else
CCP1CONbits.DC1B1 = 0x0000;
}
//************************************
// Turn ON the PWM module
//************************************
void turn_on_PWM1 (void) {
CCP1CON = CCP1CON | 0b00001100;
}
Si prega Accedi o Crea un account a partecipare alla conversazione.
- Guido
- Autore della discussione
- Platinum Member
Riduci
Di più
- Messaggi: 583
- Ringraziamenti ricevuti 1
13 Anni 6 Mesi fa #5
da Mauro Laurenti
Rendering Error in layout Message/Item: array_keys(): Argument #1 ($array) must be of type array, null given. Please enable debug mode for more information.
Risposta da Mauro Laurenti al topic Re: cerco aiuto per inserire un pulsante nel codice
Rendering Error in layout Message/Item: array_keys(): Argument #1 ($array) must be of type array, null given. Please enable debug mode for more information.
Si prega Accedi o Crea un account a partecipare alla conversazione.
Moderatori: Mauro Laurenti, Pinna, StefA, Matteo Garia
Registrati al sito
Accedi a tutte le risorse e articoli non visibili pubblicamente, puoi registrarti con pochi passi.
Login
© LaurTec 2006 - 2024