Untitled
unknown
plain_text
a year ago
4.5 kB
14
Indexable
/*---------------------------------------------------------------------------------------------------------*/
/* */
/* SPDX-License-Identifier: Apache-2.0 */
/* Copyright(c) 2020 Nuvoton Technology Corp. All rights reserved. */
/* */
/*---------------------------------------------------------------------------------------------------------*/
#include "ms51_16k.h"
//#include "stdint.h"
unsigned char xdata ADCdataAINH, ADCdataAINL;
unsigned int ADCdata = 0;
/************************************************************************************************************/
/* FUNCTION_PURPOSE: Main Loop */
/************************************************************************************************************/
//void update_PWM(unsigned int value)
//{
// PWMCON0H = (value >> 8) & 0xFF; // Set duty cycle high byte
// PWMCON0L = value & 0xFF; // Set duty cycle low byte
// set_PWMCON0_LOAD; // Load the new PWM duty cycle
//}
//unsigned int pwm_value = 0;
int16_t pwm_value = 0;
#define MAX_PWM_VALUE 65535
#define MIN_PWM_VALUE 0
void update_PWM(int16_t value)
{
PWM0H = (value >> 8) & 0xFF; // Update duty cycle high byte
PWM0L = value & 0xFF; // Update duty cycle low byte
set_PWMCON0_LOAD; // Load the new PWM duty cycle
}
void main (void)
{
/* Initial UART0 for printf */
//unsigned char temp;
MODIFY_HIRC(HIRC_24); // Set HIRC to 24 MHz
P06_QUASI_MODE;
UART_Open(24000000,UART0_Timer3,115200);
ENABLE_UART0_PRINTF; // Important! use prinft function must set TI=1;
ENABLE_ADC_AIN4;
/* ADC Low speed initial*/
ADCCON1|=0X30; /* clock divider */
ADCCON2|=0X0E; /* AQT time */
P12_PUSHPULL_MODE;
P12_QUASI_MODE;
PWM0_P12_OUTPUT_ENABLE;
PWM_IMDEPENDENT_MODE;
PWM_CLOCK_DIV_8;
PWMPH = 0x30;
PWMPL = 0x0f;
/**********************************************************************
PWM frequency = Fpwm/((PWMPH,PWMPL) + 1) <Fpwm = Fsys/PWM_CLOCK_DIV>
= (16MHz/8)/(0x7CF + 1)
= 1KHz (1ms)
***********************************************************************/
P15_INPUT_MODE; // Set P1.5 as input for button
P15_QUASI_MODE; // Set P1.5 to quasi-bidirectional mode (enables internal pull-up automatically)
P15 = 1; // Ensure P1.5 is pulled high (assuming active-low button)
P14_INPUT_MODE; // Set P1.5 as input for button
P14_QUASI_MODE; // Set P1.5 to quasi-bidirectional mode (enables internal pull-up automatically)
P14 = 1; // Ensure P1.5 is pulled high (assuming active-low button)
while(1)
{
Timer2_Delay(16000000,128,100,1000);;
clr_ADCCON0_ADCF;
set_ADCCON0_ADCS; // ADC start trig signal
while(ADCF == 0);
// ADCdata = ADCRH;
ADCdata = (ADCRH << 4) | (ADCRL & 0x0F); // Combine high and low bytes to get full 10-bit result
// printf("ADC Counts: %4d\n", ADCdata); // Print the ADC result
// Check if P14 (increment) button is pressed
if (P14 == 0)
{
if (pwm_value < MAX_PWM_VALUE)
{
pwm_value = pwm_value + 500; // Increment PWM value
update_PWM(pwm_value);
// printf("PWM Counts: %4d\n", pwm_value);
printf("PWM Counts: %u\n", pwm_value);
}
// while (P14 == 0); // Wait for button release (simple debounce)
}
// Check if P15 (decrement) button is pressed
if (P15 == 0)
{
if (pwm_value > MIN_PWM_VALUE)
{
pwm_value = pwm_value - 500;; // Decrement PWM value
update_PWM(pwm_value);
// printf("PWM Counts: %4d\n", pwm_value);
printf("PWM Counts: %u\n", pwm_value);
}
// while (P15 == 0); // Wait for button release (simple debounce)
}
//P12 ^= 1;
}
}Editor is loading...
Leave a Comment