Other Parts Discussed in Thread: EK-TM4C123GXL
Tool/software: Code Composer Studio
Hello everyone. I'm trying to create three PWM waves in CCS. Initially, the PWM wave is with 20% duty cycle; then when pressing and releasing SW1 it has to turn to 40% and when doing the same with SW2 it should turn to 80%. The "main.c" file compiles but in the debugging section it shows me "Error: Unable to read". What may be wrong with my code? All help Will be welcome.
PD: Some comments and functions are in Spanish language (I'm from Peru)
#include <stdint.h>
#include "tm4c123gh6pm.h"
#define SYS_CTRL_RCC (*((volatile unsigned long*)0x400FE060))
#define SYS_CTRL_RCGC0 (*((volatile unsigned long*)0x400FE100))
#define SYS_CTRL_RCGC2 (*((volatile unsigned long*)0x400FE108))
#define SYS_CTRL_RCGCADC (*((volatile unsigned long*)0x400FE638))
#define SYS_CTRL_PRGPIO (*((volatile unsigned long*)0x400FEA08))
#define SYS_CTRL_RCGCGPIO (*((volatile unsigned long*)0x400FE608))
#define SYS_CTRL_GPIOHBCTL (*((volatile unsigned long*)0x400FE06C))
//************************************************************************************************
//-------------------------GPIO PORTB REGISTERS---------------------------------------------------
//************************************************************************************************
#define GPIO_PORTB_DATA (*((volatile unsigned long*)0x400053FC))
#define GPIO_PORTB_DIR (*((volatile unsigned long*)0x40005400))
#define GPIO_PORTB_DEN (*((volatile unsigned long*)0x4000551C))
#define GPIO_PORTB_AFSEL (*((volatile unsigned long*)0x40005420))
#define GPIO_PORTB_PCTL (*((volatile unsigned long*)0x4000552C))
#define GPIO_PORTB_AMSEL (*((volatile unsigned long*)0x40005528))
#define SYS_CTRL_RCGC2_GPIOB 0x00000002
//************************************************************************************************
//-------------------------GPIO PORTF REGISTERS---------------------------------------------------
//************************************************************************************************
#define GPIO_PORTF_DATA (*((volatile unsigned long*)0x400253FC))
#define GPIO_PORTF_DIR (*((volatile unsigned long*)0x40025400))
#define GPIO_PORTF_DEN (*((volatile unsigned long*)0x4002551C))
#define GPIO_PORTF_AFSEL (*((volatile unsigned long*)0x40025420))
//************************************************************************************************
//-------------------------GPIO PORTE REGISTERS---------------------------------------------------
//************************************************************************************************
#define GPIO_PORTE_DATA (*((volatile unsigned long*)0x400243FC))
#define GPIO_PORTE_DIR (*((volatile unsigned long*)0x40024400))
#define GPIO_PORTE_DEN (*((volatile unsigned long*)0x4002451C))
#define GPIO_PORTE_AFSEL (*((volatile unsigned long*)0x40024420))
#define GPIO_PORTE_AMSEL (*((volatile unsigned long*)0x40024528))
//*************************************************************************************************
//------------------------PWM REGISTER-------------------------------------------------------------
//*************************************************************************************************
#define PWM_CTL_R (*((volatile unsigned long *)0x40028000))
#define PWM_SYNC_R (*((volatile unsigned long *)0x40028004))
#define PWM_ENABLE_R (*((volatile unsigned long *)0x40028008))
#define PWM_INVERT_R (*((volatile unsigned long *)0x4002800C))
#define PWM_FAULT_R (*((volatile unsigned long *)0x40028010))
#define PWM_INTEN_R (*((volatile unsigned long *)0x40028014))
#define PWM_RIS_R (*((volatile unsigned long *)0x40028018))
#define PWM_MISC_R (*((volatile unsigned long *)0x4002801C))
#define PWM_STATUS_R (*((volatile unsigned long *)0x40028020))
#define PWM_FAULTVAL_R (*((volatile unsigned long *)0x40028024))
#define PWM_ENUPD_R (*((volatile unsigned long *)0x40028028))
#define PWM0_CTL_R (*((volatile unsigned long *)0x40028040))
#define PWM0_INTEN_R (*((volatile unsigned long *)0x40028044))
#define PWM0_RIS_R (*((volatile unsigned long *)0x40028048))
#define PWM0_ISC_R (*((volatile unsigned long *)0x4002804C))
#define PWM0_LOAD_R (*((volatile unsigned long *)0x40028050))
#define PWM0_COUNT_R (*((volatile unsigned long *)0x40028054))
#define PWM0_CMPA_R (*((volatile unsigned long *)0x40028058))
#define PWM0_CMPB_R (*((volatile unsigned long *)0x4002805C))
#define PWM0_GENA_R (*((volatile unsigned long *)0x40028060))
#define PWM0_GENB_R (*((volatile unsigned long *)0x40028064))
#define PWM0_DBCTL_R (*((volatile unsigned long *)0x40028068))
#define PWM0_DBRISE_R (*((volatile unsigned long *)0x4002806C))
#define PWM0_DBFALL_R (*((volatile unsigned long *)0x40028070))
#define PWM0_FLTSRC0_R (*((volatile unsigned long *)0x40028074))
#define PWM0_FLTSRC1_R (*((volatile unsigned long *)0x40028078))
#define PWM0_MINFLTPER_R (*((volatile unsigned long *)0x4002807C))
//*************************************************************************************************
//------------------------ADC REGISTER-------------------------------------------------------------
//*************************************************************************************************
#define ADC_ACTSS_R (*((volatile unsigned long *)0x40038000))
#define ADC0_RIS_R (*((volatile unsigned long *)0x40038004))
#define ADC0_IM_R (*((volatile unsigned long *)0x40038008))
#define ADC0_ISC_R (*((volatile unsigned long *)0x4003800C))
#define ADC0_EMUX_R (*((volatile unsigned long *)0x40038014))
#define ADC0_SSPRI_R (*((volatile unsigned long *)0x40038020))
#define ADC0_PSSI_R (*((volatile unsigned long *)0x40038028))
#define ADC0_SSMUX3_R (*((volatile unsigned long *)0x400380A0))
#define ADC0_SSCTL3_R (*((volatile unsigned long *)0x400380A4))
#define ADC0_SSFIFO3_R (*((volatile unsigned long *)0x40038048))
#define ADC_ACTSS_ASEN3 0x00000008 // ADC SS3 Enable
#define ADC_RIS_INR3 0x00000008 // SS3 Raw Interrupt Status
#define ADC_IM_MASK3 0x00000008 // SS3 Interrupt Mask
#define ADC_ISC_IN3 0x00000008 // SS3 Interrupt Status and Clear
#define ADC_EMUX_EM3_M 0x0000F000 // SS3 Trigger Select mask
#define ADC_EMUX_EM3_TIMER 0x00005000 // Timer
#define ADC_SSPRI_SS3_4TH 0x00003000 // fourth priority
#define ADC_SSPRI_SS2_3RD 0x00000200 // third priority
#define ADC_SSPRI_SS1_2ND 0x00000010 // second priority
#define ADC_SSPRI_SS0_1ST 0x00000000 // first priority
#define ADC_PSSI_SS3 0x00000008 // SS3 Initiate
#define ADC_SSMUX3_MUX0_M 0x00000003 // 1st Sample Input Select mask
#define ADC_SSMUX3_MUX0_S 0 // 1st Sample Input Select lshift
#define ADC_SSCTL3_TS0 0x00000008 // 1st Sample Temp Sensor Select
#define ADC_SSCTL3_IE0 0x00000004 // 1st Sample Interrupt Enable
#define ADC_SSCTL3_END0 0x00000002 // 1st Sample is End of Sequence
#define ADC_SSCTL3_D0 0x00000001 // 1st Sample Diff Input Select
#define ADC_SSFIFO3_DATA_M 0x000003FF // Conversion Result Data mask
//*************************************************************************************************
#define NVIC_EN0_INT19 0x00080000 // Interrupt 19 enable
#define NVIC_EN0_R (*((volatile unsigned long *)0xE000E100)) // IRQ 0 to 31 Set Enable Register
#define NVIC_PRI4_R (*((volatile unsigned long *)0xE000E410)) // IRQ 16 to 19 Priority Register
#define TIMER0_CFG_R (*((volatile unsigned long *)0x40030000))
#define TIMER0_TAMR_R (*((volatile unsigned long *)0x40030004))
#define TIMER0_CTL_R (*((volatile unsigned long *)0x4003000C))
#define TIMER0_IMR_R (*((volatile unsigned long *)0x40030018))
#define TIMER0_MIS_R (*((volatile unsigned long *)0x40030020))
#define TIMER0_RIS_R (*((volatile unsigned long *)0x4003001C))
#define TIMER0_ICR_R (*((volatile unsigned long *)0x40030024))
#define TIMER0_TAILR_R (*((volatile unsigned long *)0x40030028))
#define TIMER0_TAMR_R (*((volatile unsigned long *)0x40030004))
#define TIMER0_TAPR_R (*((volatile unsigned long *)0x40030038))
#define TIMER0_TAR_R (*((volatile unsigned long *)0x40030048))
#define TIMER_CFG_16_BIT 0x00000004 // 16-bit timer configuration,
// function is controlled by bits
// 1:0 of GPTMTAMR and GPTMTBMR
#define TIMER_TAMR_TAMR_PERIOD 0x00000002 // Periodic Timer mode
#define TIMER_CTL_TAEN 0x00000001 // GPTM TimerA Enable
#define TIMER_IMR_TATOIM 0x00000001 // GPTM TimerA Time-Out Interrupt
// Mask
#define TIMER_ICR_TATOCINT 0x00000001 // GPTM TimerA Time-Out Raw
// Interrupt
#define TIMER_TAILR_TAILRL_M 0x0000FFFF // GPTM TimerA Interval Load
// Register Low
#define SYSCTL_RCGC1_R (*((volatile unsigned long *)0x400FE104))
#define SYSCTL_RCGC1_TIMER0 0x00010000 // timer 0 Clock Gating Control
#define SYS_CTRL_RCGC2_GPIOF 0x00000020
//****************************************************************************************************
//--------------RUN MODE CLOCK CONFIGURATION REGISTER (RCC) BITS DEFINITION---------------------------
//****************************************************************************************************
#define ACG 0x08000000;//Automatic Clock Gating Bit,
#define SYS_DIV 0x07800000;//Clock Divisor.As we are using PLL so MINSYSDIV used as SYS_DIV
#define USESYSDIV 0x00400000;//System Clock Divider
#define USEPWMDIV 0x00100000;//Divsion of clock on PWM clock source.
#define PWMDIV 0x000E0000;//PWM Frequency Division.
#define PWRDN 0x00002000;//Clock in POWER DOWN .
#define BYPASS 0x00000800;//PLL BYPASS bit.
#define XTAL 0x000007C0;//Crystal Oscillator value.
#define OSCSRC 0x00000030;//Oscillator Source Bit.
#define MOSCDIS 0x00000001;//Main Oscillator Disable Bit.
#define ADC_MODE0 0x00000001;
#define ADC_MODE1 0x00000002;
#define TM4C123_SW1 1<<4 // Conectado a PF4
#define TM4C123_SW2 1<<0 // Conectado a PF0
#define TWAIT 400000
#define TON 800000
void Pin_PB6_Config(void); //PB6 M0PWM0 inicialización de pines
void PWM0_EstadoInicial(void);
void config_SW1_TM4C123(void);
void config_SW2_TM4C123(void);
uint8_t lectura_sw1_tm4c123(void);
uint8_t lectura_sw2_tm4c123(void);
void PWM0_EstadoSW1(void);
void PWM0_EstadoSW2(void);
//**************************************************************************************************
//Global Flags
//**************************************************************************************************
void main(void){
void config_pines();
while(1){
if(lectura_sw1_tm4c123()){
PWM0_EstadoSW1(); // si SW1 se pulsa, la onda PWM sera de 40% DC
while(1){
if(lectura_sw2_tm4c123()){
PWM0_EstadoSW2(); // si SW2 se pulsa, la onda PWM sera de 80% DC
break;
}
}
}
}
}
void config_pines(void){
config_SW1_TM4C123();
config_SW2_TM4C123();
Pin_PB6_Config();
}
void Pin_PB6_Config(void){ //configuración pin PB6
SYS_CTRL_RCGC0|=0x00100000;//Enable PWM Clock
SYS_CTRL_PRGPIO|=(SYS_CTRL_RCGC2_GPIOB);//Enable Clock to PORTB
SYS_CTRL_RCGC2|=(SYS_CTRL_RCGC2_GPIOB);//Enable Clock to PORTB
GPIO_PORTB_AFSEL|=(0x00000040);//Enable Alternate Function on PB6
GPIO_PORTB_PCTL&=~0x0F000000;//PORT MUX CONTROL 6
GPIO_PORTB_PCTL|=0x04000000;//PORT MUX CONTROL 6
GPIO_PORTB_AMSEL&=~0x40;//Disable Analogue Functionality
SYS_CTRL_RCC|=0x00100000|(SYS_CTRL_RCC &(~0x000E0000));//Use PWMDIV, DIV_2, BYPASS=0,
GPIO_PORTB_DIR|=0x0000000F;//Make Output
GPIO_PORTB_DEN|=0x000000FF;// Digital Data is Enabled
}
void PWM0_EstadoInicial(void){ //definimos PWM con 20% DC (inicial)
// void config_timer_default(void){ // Configuración Timer 20% DC
SYSCTL_RCGC1_R |= SYSCTL_RCGC1_TIMER0; // activamos reloj del TIMER0
// SYSCTL_RCGCTIMER_R |= (1<<0); // activamos reloj del TIMER0
while(!(SYSCTL_PRTIMER_R & SYSCTL_PRTIMER_R0));//{
//SYSCTL_PRTIMER_R |= SYSCTL_PRTIMER_R0; // ponemos a 1 el bit correspondiente al registro (para TIMER0 es R0)
//}
TIMER0_CTL_R &= ~(1<<0); //deshabilitar timer A
TIMER0_CFG_R = (TIMER0_CFG_R & ~0x07) + 0x04; // modo 16 bits
TIMER0_TAMR_R = (TIMER0_TAMR_R & ~0xFFF) + 0x50A; // configuración timer A
TIMER0_CTL_R |= (1<<6); // TAPWML = 0
TIMER0_TAILR_R = (TIMER0_TAILR_R & ~(0xFFFF))|39;
TIMER0_TAPR_R = (TIMER0_TAPR_R & ~0xFF);
TIMER0_TAMATCHR_R = (TIMER0_TAMATCHR_R & ~(0xFFFF))|19;
//TIMER0_TAPMR_R =(TIMER0_TAPMR_R & ~0xFF);
}
void PWM0_EstadoSW1(void){ //definimos PWM con 40% DC (SW1)
//void config_timer_default(void){ // Configuración Timer 20% DC
SYSCTL_RCGC1_R |= SYSCTL_RCGC1_TIMER0; // activamos reloj del TIMER0
// SYSCTL_RCGCTIMER_R |= (1<<0); // activamos reloj del TIMER0
while(!(SYSCTL_PRTIMER_R & SYSCTL_PRTIMER_R0)); // ponemos a 1 el bit correspondiente al registro (para TIMER0 es R0)
TIMER0_CTL_R &= ~(1<<0); //deshabilitar timer A
TIMER0_CFG_R = (TIMER0_CFG_R & ~0x07) + 0x04; // modo 16 bits
TIMER0_TAMR_R = (TIMER0_TAMR_R & ~0xFFF) + 0x50A; // configuración timer A
TIMER0_CTL_R |= (1<<6); // TAPWML = 0
TIMER0_TAILR_R = (TIMER0_TAILR_R & ~(0xFFFF))|59;
TIMER0_TAPR_R = (TIMER0_TAPR_R & ~0xFF);
TIMER0_TAMATCHR_R = (TIMER0_TAMATCHR_R & ~(0xFFFF))|39;
//TIMER0_TAPMR_R =(TIMER0_TAPMR_R & ~0xFF);
}
void PWM0_EstadoSW2(void){ //definimos PWM con 80% DC (SW2)
//void config_timer_default(void){ // Configuración Timer 20% DC
SYSCTL_RCGC1_R |= SYSCTL_RCGC1_TIMER0; // activamos reloj del TIMER0
// SYSCTL_RCGCTIMER_R |= (1<<0); // activamos reloj del TIMER0
while(!(SYSCTL_PRTIMER_R & SYSCTL_PRTIMER_R0)); // ponemos a 1 el bit correspondiente al registro (para TIMER0 es R0)
TIMER0_CTL_R &= ~(1<<0); //deshabilitar timer A
TIMER0_CFG_R = (TIMER0_CFG_R & ~0x07) + 0x04; // modo 16 bits
TIMER0_TAMR_R = (TIMER0_TAMR_R &~0xFFF) + 0x50A; // configuración timer A
TIMER0_CTL_R |= (1<<6); // TAPWML = 0
TIMER0_TAILR_R = (TIMER0_TAILR_R & ~(0xFFFF))|99;
TIMER0_TAPR_R = (TIMER0_TAPR_R & ~0xFF);
TIMER0_TAMATCHR_R = (TIMER0_TAMATCHR_R & ~(0xFFFF))|79;
TIMER0_TAPMR_R =(TIMER0_TAPMR_R & ~0xFF);
}
void config_SW1_TM4C123(void){
SYSCTL_RCGCGPIO_R |= SYSCTL_RCGCGPIO_R5;
asm(" nop");
asm(" nop");
asm(" nop");
while(!(SYSCTL_PRGPIO_R & SYSCTL_PRGPIO_R5));
GPIO_PORTF_DIR_R &= ~(TM4C123_SW1);
GPIO_PORTF_PUR_R |= (TM4C123_SW1);
GPIO_PORTF_DEN_R |= (TM4C123_SW1);
GPIO_PORTF_PCTL_R &= ~(0x0F);
GPIO_PORTF_PCTL_R |= 0x07;
}
void config_SW2_TM4C123(void){
SYSCTL_RCGCGPIO_R |= SYSCTL_RCGCGPIO_R5;
asm(" nop");
asm(" nop");
asm(" nop");
while(!(SYSCTL_PRGPIO_R & SYSCTL_PRGPIO_R5));
GPIO_PORTF_DIR_R &= ~(TM4C123_SW2);
GPIO_PORTF_LOCK_R = 0x4C4F434B;
GPIO_PORTF_CR_R |= 0x01;
GPIO_PORTF_AFSEL_R &= ~(TM4C123_SW2);
GPIO_PORTF_PUR_R |= (TM4C123_SW2);
GPIO_PORTF_DEN_R |= (TM4C123_SW2);
GPIO_PORTF_PCTL_R &= ~(0x0F);
GPIO_PORTF_PCTL_R |= 0x07;
}
uint8_t lectura_sw1_tm4c123(void){
int contador = 0;
if(!(GPIO_PORTF_DATA_R & TM4C123_SW1)){
for(contador = 0;contador<TWAIT;contador++);
while(!(GPIO_PORTF_DATA_R & TM4C123_SW1));
for(contador = 0;contador<TWAIT;contador++);
return 1;
}
return 0;
}
uint8_t lectura_sw2_tm4c123(void){
int contador = 0;
if(!(GPIO_PORTF_DATA_R & TM4C123_SW2)){
for(contador = 0;contador<TWAIT;contador++);
while(!(GPIO_PORTF_DATA_R & TM4C123_SW2));
for(contador = 0;contador<TWAIT;contador++);
return 1;
}
return 0;
}