I'm trying to toggle an IO on an STM32H743 as fast as possible. I'm using an external 10MHz clock, powered at 3.3V, and I'm confident my main clock is running at 400MHz and the bus clock that talks to the GPIO (AHB4) is running at 200MHz. Here is some sample code I'm using to configure the chip and toggle the IO:
RCC_ClkInitTypeDef clock;
RCC_OscInitTypeDef osc;
MODIFY_REG(PWR->CR3, PWR_CR3_SCUEN, 0);
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
while (!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
// External 10.000MHz oscillator
osc.OscillatorType = RCC_OSCILLATORTYPE_HSE;
osc.HSEState = RCC_HSE_ON;
osc.HSIState = RCC_HSI_OFF;
osc.CSIState = RCC_CSI_OFF;
osc.PLL.PLLState = RCC_PLL_ON;
osc.PLL.PLLSource = RCC_PLLSOURCE_HSE;
// ((10 / 5) * 400 ) / 2 = 400
osc.PLL.PLLM = 5;
osc.PLL.PLLN = 400;
osc.PLL.PLLP = 2;
osc.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
osc.PLL.PLLRGE = RCC_PLL1VCIRANGE_3;
HAL_RCC_OscConfig(&osc);
// Sysclk source is PLL
clock.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK |
RCC_CLOCKTYPE_D1PCLK1 | RCC_CLOCKTYPE_PCLK1 |
RCC_CLOCKTYPE_PCLK2 | RCC_CLOCKTYPE_D3PCLK1);
clock.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
clock.SYSCLKDivider = RCC_SYSCLK_DIV1;
// HCLK = SYSCLK / 2 = 200
clock.AHBCLKDivider = RCC_HCLK_DIV2;
clock.APB3CLKDivider = RCC_APB3_DIV2;
clock.APB1CLKDivider = RCC_APB1_DIV2;
clock.APB2CLKDivider = RCC_APB2_DIV2;
clock.APB4CLKDivider = RCC_APB4_DIV2;
HAL_RCC_ClockConfig(&clock, FLASH_LATENCY_4);
__HAL_RCC_CSI_ENABLE();
__HAL_RCC_SYSCFG_CLK_ENABLE();
HAL_EnableCompensationCell();
// GPIOC0 is pin I'm toggling
__HAL_RCC_GPIOC_CLK_ENABLE();
GPIO_InitTypeDef gpio;
gpio.Mode = GPIO_MODE_OUTPUT_PP;
gpio.Pull = GPIO_NOPULL;
gpio.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
gpio.Pin = GPIO_PIN_0;
HAL_GPIO_Init(GPIOC, &gpio);
// Configure GPIOC9 as SYSCLK/2 (200MHz)
gpio.Mode = GPIO_MODE_AF_PP;
gpio.Alternate = GPIO_AF0_MCO;
gpio.Pull = GPIO_NOPULL;
gpio.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
gpio.Pin = GPIO_PIN_9;
HAL_GPIO_Init(GPIOC, &gpio);
HAL_RCC_MCOConfig(RCC_MCO2, RCC_MCO2SOURCE_SYSCLK, RCC_MCODIV_2);
while (1)
{
GPIOC->ODR = 0;
GPIOC->ODR = 1;
GPIOC->ODR = 0;
GPIOC->ODR = 1;
GPIOC->ODR = 0;
GPIOC->ODR = 1;
GPIOC->ODR = 0;
GPIOC->ODR = 1;
GPIOC->ODR = 0;
GPIOC->ODR = 1;
GPIOC->ODR = 0;
GPIOC->ODR = 1;
GPIOC->ODR = 0;
GPIOC->ODR = 1;
GPIOC->ODR = 0;
GPIOC->ODR = 1;
GPIOC->ODR = 0;
GPIOC->ODR = 1;
GPIOC->ODR = 0;
GPIOC->ODR = 1;
}
When I look at this on an oscilloscope I see my SYSCLK/2 correctly at 200MHz but the toggle pin is only toggling once every 60nS.
When I look at the disassembly for this code (compiled "-O3"), the toggling is being compiled in to single STR instructions (r1 = 0, r2 = 1, r3 = &GPIOC->ODR):
...
str r1, [r3, #20]
str r2, [r3, #20]
str r1, [r3, #20]
str r2, [r3, #20]
str r1, [r3, #20]
str r2, [r3, #20]
str r1, [r3, #20]
str r2, [r3, #20]
str r1, [r3, #20]
str r2, [r3, #20]
str r1, [r3, #20]
...
I can't find cycle information for the Cortex-M7 processor, but when I look at the cycle time for the Cortex-M4 processor (http://infocenter.arm.com/help/topic/com.arm.doc.ddi0439b/DDI0439B_cortex_m4_r0p0_trm.pdf) table 3-1, I see a STR should take two clock cycles to execute. I would expect to see my IO toggling aproimately every 10 nanoseconds (or every two clock cycles on the 200MHz AHB4 bus).
I've tried running the code from FLASH and SRAM, but there is no difference in IO speed.
Why isn't my IO toggling every two clock cycles?
EDIT:
In the reference manual (st.com/resource/en/reference_manual/dm00314099.pdf) section 10.2 it says, "Fast toggle capable of changing every two clock cycles."
EDIT:
So some comments have mentioned that the processor isn't really meant to talk directly to GPIO. So I've rewritten the code to use DMA (I had to use BDMA and specifically allocate the data in RAM_D3 to get it to run as fast as the software loop). Ultimately I want to clock out calculated data from RAM to the entire GPIO port. How can I clock data out every two clock cycles as the reference manual suggests?
DMA code that runs on a NUCLEO-H743ZI:
#include <stm32h7xx_hal.h>
#include <stdint.h>
DMA_HandleTypeDef dma;
void SysTick_Handler(void)
{
HAL_IncTick();
}
static void ClockConfig(void)
{
RCC_ClkInitTypeDef clock;
RCC_OscInitTypeDef osc;
MODIFY_REG(PWR->CR3, PWR_CR3_SCUEN, 0);
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
while (!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
osc.OscillatorType = RCC_OSCILLATORTYPE_CSI;
osc.CSIState = RCC_CSI_ON;
osc.CSICalibrationValue = 16;
osc.PLL.PLLState = RCC_PLL_ON;
osc.PLL.PLLSource = RCC_PLLSOURCE_CSI;
osc.PLL.PLLM = 1;
osc.PLL.PLLN = 200;
osc.PLL.PLLP = 2;
osc.PLL.PLLQ = 2;
osc.PLL.PLLR = 2;
osc.PLL.PLLRGE = RCC_PLL1VCIRANGE_2;
osc.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
osc.PLL.PLLFRACN = 0;
HAL_RCC_OscConfig(&osc);
clock.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_D1PCLK1 | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2 | RCC_CLOCKTYPE_D3PCLK1);
clock.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
clock.SYSCLKDivider = RCC_SYSCLK_DIV1;
clock.AHBCLKDivider = RCC_HCLK_DIV2;
clock.APB3CLKDivider = RCC_APB3_DIV2;
clock.APB1CLKDivider = RCC_APB1_DIV2;
clock.APB2CLKDivider = RCC_APB2_DIV2;
clock.APB4CLKDivider = RCC_APB4_DIV2;
HAL_RCC_ClockConfig(&clock, FLASH_LATENCY_4);
__HAL_RCC_CSI_ENABLE();
__HAL_RCC_SYSCFG_CLK_ENABLE();
HAL_EnableCompensationCell();
HAL_SYSTICK_Config(SystemCoreClock / 1000);
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}
void GpioConfig(void)
{
__GPIOC_CLK_ENABLE();
GPIO_InitTypeDef gpio;
gpio.Pin = GPIO_PIN_0;
gpio.Mode = GPIO_MODE_OUTPUT_PP;
gpio.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
gpio.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOC, &gpio);
gpio.Pin = GPIO_PIN_1;
gpio.Mode = GPIO_MODE_OUTPUT_PP;
gpio.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
gpio.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOC, &gpio);
gpio.Mode = GPIO_MODE_AF_PP;
gpio.Alternate = GPIO_AF0_MCO;
gpio.Pull = GPIO_NOPULL;
gpio.Speed = GPIO_SPEED_FAST;
gpio.Pin = GPIO_PIN_9;
HAL_GPIO_Init(GPIOC, &gpio);
HAL_RCC_MCOConfig(RCC_MCO2, RCC_MCO2SOURCE_SYSCLK, RCC_MCODIV_2);
}
uint8_t complete = 0;
void DmaComplete(DMA_HandleTypeDef *handle)
{
complete = 1;
}
void DmaError(DMA_HandleTypeDef *handle)
{
complete = 1;
}
void DmaConfig(void)
{
__HAL_RCC_BDMA_CLK_ENABLE();
dma.Instance = BDMA_Channel0;
dma.Init.Request = DMA_REQUEST_MEM2MEM;
dma.Init.Direction = DMA_MEMORY_TO_MEMORY;
dma.Init.PeriphInc = DMA_PINC_ENABLE;
dma.Init.MemInc = DMA_MINC_DISABLE;
dma.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
dma.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
dma.Init.Mode = DMA_NORMAL;
dma.Init.Priority = DMA_PRIORITY_VERY_HIGH;
HAL_DMA_Init(&dma);
HAL_DMA_RegisterCallback(&dma, HAL_DMA_XFER_CPLT_CB_ID, DmaComplete);
HAL_DMA_RegisterCallback(&dma, HAL_DMA_XFER_ERROR_CB_ID, DmaError);
HAL_NVIC_SetPriority(BDMA_Channel0_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(BDMA_Channel0_IRQn);
}
void BDMA_Channel0_IRQHandler(void)
{
HAL_DMA_IRQHandler(&dma);
}
void HardFault_Handler(void)
{
__NOP();
}
__attribute__((section(".ram_d3")))
static uint16_t src[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int main(void)
{
HAL_Init();
ClockConfig();
GpioConfig();
DmaConfig();
while (1)
{
complete = 0;
HAL_DMA_Start_IT(&dma, (uint32_t)&src, (uint32_t)&GPIOC->ODR, 10);
while (complete == 0) ;
}
}
Your assumptions are wrong. The GPIO speed is not determined by the core instructions timings, but by the physical characteristics of the peripheral. See the table in the DS where static and dynamic GPIO port characteristics are. The maximum speed depends on many parameters.
Related
Using MSP430F6736A embedded programming. Code written in Code Composer Studio. I have a problem in my code, but i have no idea what i can do to fix it.
When I compile the program in code composer, works well but UART are sending to PC just ".ø€.ø€.€€x" that means i'm getting only strange charracters
#include "msp430g2553.h"
#include <stdio.h>
float RLDR=0, lux=0, v0=0;
unsigned int ADC10_vetor[16]={0};
unsigned int sum=0;
unsigned char i=0;
unsigned char TX_data[32], tx_index = 0, lux_int =0;
void ini_ucon(void);
void ini_p1_p2(void);
void ini_Timer0(void);
void ini_ADC10(void);
void ini_USCI_UART(void);
void ini_ucon(void){
WDTCTL = WDTPW | WDTHOLD;
DCOCTL = CALDCO_8MHZ;
BCSCTL1 = CALBC1_8MHZ;
BCSCTL2 = DIVS0 + DIVS1; //SMCLK = 1MHz
__enable_interrupt();
}
void ini_p1_p2(void){
P1DIR = 0xFF;
P1OUT = 0x00;
P2DIR = 0xFF;
P2OUT = 0x00;
}
void ini_Timer0(void){
TA0CTL = TASSEL1 + ID1 + ID0 + MC0; //SMCLK = 1MHz/8 = 125000
TA0CCTL1 = OUTMOD0 + OUTMOD1 + OUTMOD2 + OUT;
TA0CCR0 = 62499; //2Hz = 0.5 secs. , TA0CCR0 = 0.5*125000 - 1 = 62499
TA0CCR1 = 31249; //PWM = 50%
}
void ini_ADC10(void){
ADC10CTL0 = ADC10SHT1 + MSC + ADC10ON + ADC10IE;
ADC10CTL1 = INCH0 + INCH2 + SHS0 + ADC10SSEL1 + CONSEQ1;
ADC10AE0 = BIT5; //P1.5 para A5
ADC10DTC0 = 0;
ADC10DTC1 = 16;
ADC10SA = &ADC10_vetor[0];
ADC10CTL0 |= ENC;
}
int main(void){
ini_ucon();
ini_p1_p2();
ini_Timer0();
ini_ADC10();
ini_USCI_UART();
while(1){
}
}
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_RTI(void){
ADC10CTL0 &= ~ENC;
soma = 0;
for(i=0;i<16;i++){
sum = sum + ADC10_vetor[i];
}
sum = sum >> 4;
//RLDR = (42625000)/(3*sum)-10000;
//lux = 500/(RLDR/1000);
//v0=5*(sum/(sum+10000));
v0=sum*(3.6/1024);
RLDR = (10000*(5-v0))/v0;
lux = (500/(RLDR/1000));
lux_int = (unsigned char) lux;
sprintf(&TX_data[0], "LUX = %d \n", lux_int);
UCA0TXBUF = TX_data[0];
tx_index++;
ADC10SA = &ADC10_vetor[0];
ADC10CTL0 |= ENC;
}
#pragma vector=USCIAB0TX_VECTOR
__interrupt void RTI_USCI_UART(void)
{
IFG2 &= ~UCA0TXIFG;
if(TX_data[tx_index] == '\0'){
tx_index = 0;
}else{
if(tx_index >= 32){
tx_index = 0;
}else{
UCA0TXBUF = TX_data[tx_index];
tx_index++;
}
}
}
void ini_USCI_UART(void){
UCA0CTL1 |= UCSWRST;
UCA0CTL0 = 0;
UCA0CTL1 = UCSSEL0 + UCSSEL1 + UCSWRST;
UCA0BR0 = 0xA0;
UCA0BR1 = 0x01;
UCA0MCTL = UCBRS1 + UCBRS2;
UCA0STAT = 0;
P1SEL |= BIT2;
P1SEL2 |= BIT2;
UCA0CTL1 &= ~UCSWRST;
IFG2 &= ~UCA0TXIFG;
IE2 |= UCA0TXIE;
}
Does anyone have an idea how to fix this?
I am using MPLAB XC8 to program pic18f45k22 to read data from 2 thermocouples. The code showed 2 problems, The first is that during simulation, the data from the sensor is shifted (sent to the second address)
for Example: if I have 2 addresses 0x0D and 0x0F, and 2 values 100 and 95, and 100 should got to 0x0D and 95 to 0x0F. However 100 goes to 0x0F and 95 to 0x0D.
**The second ** appears after uploading the code to the MCU, and it is that the MCU cannot Read any data from the sensors.
this is the main code:
void main(void) {
//SPI configuration
spiInit();
ANSELB = 0x00; //PORTB bits as digital
TRISB3 = 0; //GPIO as CS output
TRISB4 = 0;
TRISB5 = 0;
LATBbits.LATB3 = 1;
LATBbits.LATB4 = 1;
LATBbits.LATB5 = 1;
timer1init(); //initiate timer1
uartinit(9600); //initiate UART
__delay_ms(100);
while(1){
switch (cnt){ //timer interrupt routine every 1 second
//thermocouple code
case 50:
LATB3 = 0; //turn on CS
thercoup1 = spiRead(0)<<8; //Read first byte
thercoup1 |= spiRead(0); //add second byte with first one
LATB3 = 1; //turn off CS1
thercoup1 = (thercoup1>>4);
//thercoup1 = (thercoup1>>3)*0.25; //another method for calculating thermocouple value
LATB4 = 0; //turn on CS1
thercoup2 = spiRead(0)<<8; //Read first byte
thercoup2 |= spiRead(0); //add second byte with first one
LATB4 = 1; //turn off CS1
thercoup2 = (thercoup2>>4); //right shift of the reading
HMT_WriteVPN16(0x08000C, thercoup1); //send thermocouple1 data to 0x08000C
HMT_WriteVPN16(0x08000E, thercoup2); //send thermocouple2 data to 0x08000E
cnt = 0; //reset timer
break;
}
}
}
void __interrupt() ISR (void){
timer();
UART_Read();
}
And This is the configuration code of SPI module.
void spiInit(void)
{
ANSELC = 0x00;
SSP1STATbits.SMP = 0; //input Data is sampled at the middle
SSP1STATbits.CKE = 0; //Transmission occurs from Idle to Active
//Master mode Fosc/4
SSP1CON1bits.SSPM0 = 0;
SSP1CON1bits.SSPM1 = 0;
SSP1CON1bits.SSPM2 = 0;
SSP1CON1bits.SSPM3 = 0;
//
SSP1CON1bits.CKP = 1; //clock polarity is high
SSP1CON1bits.SSPEN = 1;//Serial communication is Enabled
SSP1CON1bits.SSPOV = 0;//Overflow is off
SSP1CON1bits.WCOL = 0;//Write collision is off
TRISCbits.RC5 = 1; //SDO is Disabled
TRISCbits.RC4 = 1; //SDI is Enabled
TRISCbits.RC3 = 0; //SCK is Enabled
}
unsigned short spiRead(unsigned char dat) //REad the received data
{
SSPBUF= dat; //the data loaded on the SSPBUF are not important.
while(!SSPSTATbits.BF); //save the received data from the slave.
return(SSPBUF);
}
Im using the PIC 18 microcontroller to control the speed of a DC Motor using PWM. I have managed to get it to spin using the code below. And I have tested that my H-Bridge is 100% functional.
However, when I switch on my circuit, 12V to the Motor and 5V to the logic, And I send a command to the circuit using my RS232 communication module, (which I have tested and it recieves and transmits correctly), the program resets and the current on the bench power supply falls to 0A. Sometimes the motor jerks slighty, almost as if its trying to spin, but then stops.
Any ideas where I could be going wrong?
/*
* File: serial.c
* Author: Chris Lombaard
*
* Created on September 13, 2013, 2:39 PM
*/
#pragma config FOSC = INTIO7 // Oscillator Selection bits (Internal oscillator block, CLKOUT function on OSC2)
#pragma config PLLCFG = OFF // 4X PLL Enable (Oscillator used directly)
#pragma config PRICLKEN = ON // Primary clock enable bit (Primary clock is always enabled)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)
#pragma config PWRTEN = OFF // Power-up Timer Enable bit (Power up timer disabled)
#pragma config BOREN = SBORDIS // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))
#pragma config BORV = 190 // Brown Out Reset Voltage bits (VBOR set to 1.90 V nominal)
#pragma config WDTEN = OFF // Watchdog Timer Enable bits (WDT is always enabled. SWDTEN bit has no effect)
#pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768)
#pragma config CCP2MX = PORTC1 // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = ON // PORTB A/D Enable bit (PORTB<5:0> pins are configured as analog input channels on Reset)
#pragma config CCP3MX = PORTB5 // P3A/CCP3 Mux bit (P3A/CCP3 input/output is multiplexed with RB5)
#pragma config HFOFST = ON // HFINTOSC Fast Start-up (HFINTOSC output and ready status are not delayed by the oscillator stable status)
#pragma config T3CMX = PORTC0 // Timer3 Clock input mux bit (T3CKI is on RC0)
#pragma config P2BMX = PORTD2 // ECCP2 B output mux bit (P2B is on RD2)
#pragma config MCLRE = EXTMCLR // MCLR Pin Enable bit (MCLR pin enabled, RE3 input pin disabled)
#pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = OFF // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled if MCLRE is also 1)
#pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
#pragma config CP0 = OFF // Code Protection Block 0 (Block 0 (000800-001FFFh) not code-protected)
#pragma config CP1 = OFF // Code Protection Block 1 (Block 1 (002000-003FFFh) not code-protected)
#pragma config CP2 = OFF // Code Protection Block 2 (Block 2 (004000-005FFFh) not code-protected)
#pragma config CP3 = OFF // Code Protection Block 3 (Block 3 (006000-007FFFh) not code-protected)
#pragma config CPB = OFF // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)
#pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM not code-protected)
#pragma config WRT0 = OFF // Write Protection Block 0 (Block 0 (000800-001FFFh) not write-protected)
#pragma config WRT1 = OFF // Write Protection Block 1 (Block 1 (002000-003FFFh) not write-protected)
#pragma config WRT2 = OFF // Write Protection Block 2 (Block 2 (004000-005FFFh) not write-protected)
#pragma config WRT3 = OFF // Write Protection Block 3 (Block 3 (006000-007FFFh) not write-protected)
#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot Block (000000-0007FFh) not write-protected)
#pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM not write-protected)
#pragma config EBTR0 = OFF // Table Read Protection Block 0 (Block 0 (000800-001FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF // Table Read Protection Block 1 (Block 1 (002000-003FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF // Table Read Protection Block 2 (Block 2 (004000-005FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF // Table Read Protection Block 3 (Block 3 (006000-007FFFh) not protected from table reads executed in other blocks)
#pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot Block (000000-0007FFh) not protected from table reads executed in other blocks)
#include <P18F45K22.h>
#include <xc.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define _XTAL_FREQ 4000000
#define length(x) (sizeof(x) / sizeof(x[0])) //Length of array
void writeUART(const unsigned char string[]);
void setup(void);
void inputCheck(void);
void stepF(void);
void stepB(void);
unsigned char buffer[8] = {'\0'};
unsigned char prevPos = 0;
unsigned char currPos = 0;
unsigned char bufferLength;
unsigned char direction = 0;
unsigned char speed = 0;
unsigned char isSetup = 0;
char main(void){
if(isSetup == 0){
setup();
writeUART("ERD 320 Practical 2 ----- Group 1\n");
writeUART("JC Lombaard - 11028786\n");
writeUART("VFDC Henriques - 11100232\n");
writeUART("William Reeler - 11228866\n");
writeUART("FAN POSITION: 1");
PORTDbits.RD1 = 1;
PORTDbits.RD0 = 0;
PORTCbits.RC3 = 0;
PORTCbits.RC0 = 0;
PORTAbits.RA6 = 0;
}
while(1){
if(PORTEbits.RE1 == 1)
stepB();
if(PORTEbits.RE0 == 1){
writeUART("\nFan calibrated!\n");
break;
}
}
bufferLength = 0;
while (1);
return (EXIT_SUCCESS);
}
void interrupt high_priority isr_high(void) {
if(PIR1bits.RC1IF == 1){
if(RCREG1 == '\n'){
buffer[bufferLength++] = '\0';
bufferLength = 0;
inputCheck();
}else{
buffer[bufferLength++] = RCREG1;
PIR1bits.RC1IF = 0;
}
}
}
void interrupt low_priority isr_low(void){
PIR1bits.TMR2IF = 0;
TMR2 = 0;
}
void inputCheck(void) {
const unsigned char commands[11][3] = {"S0", "S1", "S2", "S3", "S4", "P1", "P2", "P3", "P4", "R", "F"};
unsigned char choice = 0;
for(; choice < 11; choice++)
if (strcmp(buffer, commands[choice]) == 0){
break;
}
switch(choice){
case 0:
writeUART("FAN SPEED: 0% DC");
PORTA = 0b00111111;
speed = 0;
if(direction == 0){
CCPR1L = 0x00;
}else{
CCPR2L = 0x00;
}
break;
case 1:
writeUART("FAN SPEED: 10% DC");
PORTA = 0b00000110;
speed = 0b01101110 ;
if(direction == 0){
CCPR1L = 0b11010000 ;
__delay_ms(100);
CCPR1L = 0b01101110 ;
}else{
CCPR2L = 0b11010000 ;
__delay_ms(100);
CCPR2L = 0b01101110 ;
}
break;
case 2:
writeUART("FAN SPEED: 30% DC");
PORTA = 0b01011011;
speed = 0b10001100;
if(direction == 0){
CCPR1L = 0b11010000;
__delay_ms(100);
CCPR1L = 0b10001100;
}else{
CCPR2L = 0b11010000 ;
__delay_ms(100);
CCPR2L = 0b10001100 ;
}
break;
case 3:
writeUART("FAN SPEED: 60% DC");
PORTA = 0b01001111;
speed = 0b10101101;
if(direction == 0){
CCPR1L = 0b11010000 ;
__delay_ms(100);
CCPR1L = 0b10101101 ;
}else{
CCPR2L = 0b11010000 ;
__delay_ms(100);
CCPR2L = 0b10101101 ;
}
break;
case 4:
writeUART("FAN SPEED: 90% DC");
PORTA = 0b01100110;
speed = 0b11010000 ;
if(direction == 0){
CCPR1L = 0b11010000;
}else{
CCPR2L = 0b11010000;
}
break;
case 5:
currPos = 1;
if(prevPos > currPos){
for(int i = prevPos+1; i > currPos; i--)
stepB();
}else{
}
writeUART("FAN POSITION: 1");
PORTDbits.RD1 = 1;
PORTDbits.RD0 = 0;
PORTCbits.RC3 = 0;
PORTCbits.RC0 = 0;
prevPos = currPos;
break;
case 6:
prevPos = currPos;
currPos = 2;
if(prevPos > currPos){
for(int i = prevPos+1; i > currPos; i--)
stepB();
}else{
for(int i = currPos+1; i > prevPos; i--)
stepF();
}
writeUART("FAN POSITION: 2");
PORTDbits.RD1 = 0;
PORTDbits.RD0 = 1;
PORTCbits.RC3 = 0;
PORTCbits.RC0 = 0;
prevPos = currPos;
break;
case 7:
prevPos = currPos;
currPos = 3;
if(prevPos > currPos){
for(int i = prevPos+1; i > currPos; i--)
stepB();
}else{
for(int i = currPos+1; i > prevPos; i--)
stepF();
}
writeUART("FAN POSITION: 3");
PORTDbits.RD1 = 0;
PORTDbits.RD0 = 0;
PORTCbits.RC3 = 1;
PORTCbits.RC0 = 0;
prevPos = currPos;
break;
case 8:
prevPos = currPos;
currPos = 4;
if(prevPos > currPos){
for(int i = prevPos+1; i > currPos; i--)
stepB();
}else{
for(int i = currPos+1; i > prevPos; i--)
stepF();
}
writeUART("FAN POSITION: 4");
PORTDbits.RD1 = 0;
PORTDbits.RD0 = 0;
PORTCbits.RC3 = 0;
PORTCbits.RC0 = 1;
prevPos = currPos;
break;
case 9:
direction = 1;
CCP1CON = 0b00000000;
CCP2CON = 0b00111100;
CCPR2L = speed;
writeUART("FAN DIRECTION: REVERSED");
break;
case 10:
direction = 0;
CCP1CON = 0b00111100;
CCP2CON = 0b00000000;
CCPR1L = speed;
writeUART("FAN DIRECTION: FORWARD");
break;
default:
break;
}
}
void stepF(void){
for(int i = 0; i < 1; i++){
PORTB = 0b0001;
__delay_ms(100); //Delay between transitions
PORTB = 0b0010;
__delay_ms(100); //Delay between transitions
PORTB = 0b0100;
__delay_ms(100); //Delay between transitions
PORTB = 0b1000;
__delay_ms(100); //Delay between transitions
}
}
void stepB(void){
for(int i = 0; i < 1; i++){
PORTB = 0b1000;
__delay_ms(100); //Delay between transitions
PORTB = 0b0100;
__delay_ms(100); //Delay between transitions
PORTB = 0b0010;
__delay_ms(100); //Delay between transitions
PORTB = 0b0001;
__delay_ms(100); //Delay between transitions
}
}
void defaultPos(void){
PORTB = 0b1000;
__delay_ms(100); //Delay between transitions
PORTB = 0b0100;
__delay_ms(100); //Delay between transitions
PORTB = 0b0010;
__delay_ms(100); //Delay between transitions
PORTB = 0b0001;
__delay_ms(100); //Delay between transitions
}
void writeUART(const unsigned char string[]){
for(unsigned char j = 0; j < strlen(string); j++){
TXREG1 = string[j];
__delay_us(1000);
}
}
void setup(void){
isSetup = 1;
//PORTC
PORTC = 0x00;
LATC = 0x00;
TRISC = 0xC0; //Set RC6 & RC7 as inputs for EUSART
TRISCbits.RC6 = 0;
TRISCbits.RC7 = 1;
ANSELC = 0x00;
//PORTD
PORTD = 0x00;
LATD = 0x00;
TRISD = 0x00;
ANSELD = 0x00;
//PORTE
PORTE = 0x00;
LATE = 0x00;
TRISEbits.RE0 = 1;
TRISEbits.RE1 = 1;
ANSELE = 0x00;
//PORTB
PORTB = 0x00;
LATB = 0x00;
TRISB = 0x00;
ANSELB = 0x00;
PORTA = 0x00;
LATA = 0x00;
TRISA = 0x00;
ANSELA = 0x00;
//Oscillator
OSCCON = 0b01011100; //4 MHz oscillator
//EUSART
TXSTA1bits.BRGH = 1; //Highspeed baudrate
BAUDCON1bits.BRG16 = 0;
SPBRG1 = 12; //Baudrate of 19230 (FOSC = 4 MHz, BRGH = 1, BRG16 = 0)
TXSTA1bits.SYNC = 0; //Asynchronous
RCSTA1bits.SPEN = 1; //Enable rx & tx pins as serial pins
RCSTA1bits.CREN = 1; //Enable continuous reception, enable receiver
TXSTA1bits.TXEN = 1; //Enable transmitter
TXREG1 = 0x00;
RCREG1 = 0x00;
//Interrupts
RCONbits.IPEN = 1; //Enable priorities
INTCONbits.GIE_GIEH = 1; //Enable high priority interrupts
INTCONbits.PEIE_GIEL = 1; //Enable low priority interrupts
PIE1bits.TMR2IE = 1;
IPR1bits.TMR2IP = 0;
PIE1bits.RC1IE = 1; //Enable RX interrupt
PIR1bits.RC1IF = 0; //Clear interrupt flag
IPR1bits.RC1IP = 1; //High priority for RX interrupts
//PWM
PR2 = 0b11111001 ;
T2CON = 0b00000100; //1 KHz pulse frequency on CCP1 pin
CCPR1L = 0x00;
CCPR2L = 0x00;
CCP1CON = 0b00111100;
CCP2CON = 0b00000000;
TMR2 = 0;
}
A few suggestions,
Oscilliscope?
test with a light bulb rather than a DC motor, less current and voltage drop
this smells like a hardware problem
I've been trying to use the PWM module on a PIC16F877 MCU but all I get is a flat low level on both CCP1/CCP2 pins.
The code configuring and starting the PWM module is the following.
// Configure PWM
// Timer 2 (PWM timebase)
TMR2 = 0; //Clear timer
TOUTPS0 = 0;
TOUTPS1 = 0;
TOUTPS2 = 0;
TOUTPS3 = 1; //Postscaler -> 8 (previously set to 0)
T2CKPS0 = 0;
T2CKPS1 = 1; //Prescaler -> 16
TMR2IF = 0;
TMR2IE = 1; //Interrupt
PR2 = 233; //~2.5ms
//PWM1 config
CCPR1L = 0x0F;
CCP1X = 0;
CCP1Y = 0; //PWM1 duty cycle
TRISB2 = 0; //CCP1 pin is output (Error is here, see below)
TMR2ON = 1; //Enable timer
CCP1CON = 0x0c; //CPP1 is a PWM
Any code (PICC) which can successfully start the PWM on pic16 devices whould be useful.
CCP1 Pin was not being correctly set. 'TRISB2 = 0' should be 'TRISC2 = 0'
// Timer 2 (PWM timebase)
TMR2 = 0; //Clear timer
TOUTPS0 = 0;
TOUTPS1 = 0;
TOUTPS2 = 0;
TOUTPS3 = 1; //Postscaler -> 8
T2CKPS0 = 0;
T2CKPS1 = 1; //Prescaler -> 16
TMR2IF = 0;
TMR2IE = 1; //Interrupt
PR2 = 233; //~2.5ms
//PWM1 config
CCPR1L = 0xFF;
CCP1X = 1;
CCP1Y = 1; //PWM1 duty cycle
TRISC2 = 0; //Previously was TRISB2
TMR2ON = 1;
CCP1CON = 0x0c; //CPP1 is a PWM
I'm using the pic18F4550 with microchip v8.63 and with the C 18 compiler. I'm using a LDR that retrieve the value of the led (not on my picdem board) (red, green and blue) these values are stored in a variable after each conversion. Afther that when I press the button S2, I come into the method ISR: this part works.
But now: I try to compare the variable red, green and blue in the if's: but I think that it not happen, he just go to my 'else' (led RB3 on my picdem board burns).
#include <p18f4550.h>
/** V E C T O R R E M A P P I N G *******************************************/
extern void _startup (void); // See c018i.c in your C18 compiler dir
#pragma code _RESET_INTERRUPT_VECTOR = 0x001000
void _reset (void)
{
_asm goto _startup _endasm
}
#pragma code
void ISR (void);
#pragma code _HIGH_INTERRUPT_VECTOR = 0x001008
void _high_ISR (void)
{
_asm goto ISR _endasm
}
#pragma code _LOW_INTERRUPT_VECTOR = 0x001018
void _low_ISR (void)
{
;
}
#pragma code
/******************************************************************************/
// global variable, value off LDR.
unsigned int var1ADRESH = 0x00;
unsigned int color_red = 0;
unsigned int color_green = 0;
unsigned int color_blue = 0;
void main (void)
{
TRISD = 0x00; // PORTD als uitgang
RCONbits.IPEN = 0; // prioriteit uit
INTCONbits.GIE = 1; // enable interrupt
INTCONbits.RBIE = 1; // interrupt portB aan
//= set up port =
TRISAbits.TRISA0 = 1; // Set RA0/AN0 to input
//leds
TRISAbits.TRISA3 = 0;
TRISAbits.TRISA4 = 0;
TRISAbits.TRISA5 = 0;
LATAbits.LATA3 = 1;
LATAbits.LATA4 = 1;
LATAbits.LATA5 = 1;
ADCON0 = 0b00000000; // Set channel select to AN0
ADCON1 = 0b00001110; // Configure RA0/AN0 as analogue
ADCON2 = 0b10101010; // Right justified result
// TAD 12 and FOSC 32 - may need to adjust this
// depending on your clock frequency (see datasheet)
while(1)
{
_asm sleep _endasm
}
}
#pragma interrupt ISR
void ISR (void)
{
if (INTCONbits.RBIF==1) {
//conversie blauw
LATAbits.LATA3 = 0;
ADCON0bits.ADON = 1; // Enable ADC
// read LDR value.
ADCON0bits.GO = 1; // Set the GO bit of the ADCON0 register to start the conversion.
while (ADCON0bits.GO); // Wait until the conversion is complete.
ADCON2bits.ADFM = 0; // read result as 8-bit. (dus data in ADRESH) !
//= read data in ADRESH =
var1ADRESH = ADRESH; // reading value from LDR
color_blue = ADRESH; //waarde in blauw
//conversie rood
LATAbits.LATA3 = 1;
LATAbits.LATA4 = 0;
ADCON0bits.ADON = 1; // Enable ADC
// read LDR value.
ADCON0bits.GO = 1; // Set the GO bit of the ADCON0 register to start the conversion.
while (ADCON0bits.GO); // Wait until the conversion is complete.
ADCON2bits.ADFM = 0; // read result as 8-bit. (dus data in ADRESH) !
//= read data in ADRESH =
var1ADRESH = ADRESH; // reading value from LDR
color_red = ADRESH; //waarde in blauwe steken
//conversie groen
LATAbits.LATA4 = 1;
LATAbits.LATA5 = 0;
ADCON0bits.ADON = 1; // Enable ADC
// read LDR value.
ADCON0bits.GO = 1; // Set the GO bit of the ADCON0 register to start the conversion.
while (ADCON0bits.GO); // Wait until the conversion is complete.
ADCON2bits.ADFM = 0; // read result as 8-bit. (dus data in ADRESH) !
//= read data in ADRESH =
var1ADRESH = ADRESH; // reading value from LDR
color_green = ADRESH; //waarde in blauwe steken
// alles uitzetten
//PORTB = 0b1111111;
LATAbits.LATA5 = 1;
if(color_blue > color_red && color_blue > color_green){
//blauw
LATDbits.LATD0 = 1;
}
if(color_red > color_blue && color_red > color_green){
//rood
LATDbits.LATD1 = 1;
}
if(color_green > color_red && color_green > color_blue){
//groen
LATDbits.LATD2 = 1;
}
else {
LATDbits.LATD3 = 1;
}
}
INTCONbits.RBIF = 0;
}
Try to set all three of LATA3,4,5 before every readout?
Set TRIS for D0..2
I detect a few weird things (but I confess none might be the source).
Once you set the LATA bits, does the LDR immediately changes val? Of course not, but is it a matter of ns, us or ms?... I don't see any delays for LDR settling times.
You are always sleeping so it's probably required to set the ADON=1 (check DS). However, check the device DS for how long does the AD needs to get it's internals up and ready after setting ADON=1. On that subject, once ADON=1 you shouldn't and don't need to set it over again the the next two acquisitions.
The same thing about the format of the result. Set it once, and do it before the GO=1. Don't re-set every conversion.
If you're using only 8bit results, why the unsigned int? (btw, int is pretty dangerous in microcontrollers. Use short and chars and always explicit signdness).
Why the var1ADRESH assignment then color_xxx? when do the LATD bits eventually get =0?
Your If else only works on the last branch. I believe you intended a if{} else if{} else if... structure.