
1个回答
展开全部
#include "16F877.h"
#device ADC = 8 // 一个8位寄存器ADC模式
#fuses HS, NOWDT, PUT, NOPROTECT, BROWNOUT, NODEBUG, NOLVP // High-Speed 20MHz, No Watchdog, No Protection, Brownout Protection,
#use delay(clock=20000000) // 20MHz Crystal
//int is defined as 8-bit unsigned integer using CCS compiler
void main (void)
{
signed int direction;
int delta;
int pwm;
int upperbound;
int lowerbound;
float power;
float powerold;
float voltage;
float voltagedrop;
float voltagedifference;
float currentma;
float measuredvoltage;
float measuredvoltagedrop;
direction = 1; // Set initial direction to positive
delta = 1; // Amount by which to adjust the PWM - 7-bit resolution so duty step of 2%
pwm = 26; // Initial position of the PWM - 50% Duty Cycle with 7-bit resolution.
upperbound = 49; // Upper bound of the PWM %
lowerbound = 1; // Lower bound of the PWM %
power = 0; // Initial Value of Power
setup_adc(ADC_CLOCK_DIV_32); // ADC clock
setup_adc_ports(ALL_ANALOG); // Set all inputs to analog
output_low(PIN_C1); // Set CCP1 output low
setup_ccp1(ccp_pwm); // setup pin CCP1 (RC2) to do PWM
setup_timer_2(T2_DIV_BY_1,12,1); // 384.615kHz
while (1)
{
//delay_ms(1000) // Wait 1 Second
set_adc_channel(0); // Select RA0
//delay_ms(20); // Wait to Read ADC
measuredvoltage = read_adc(); // Read the voltage input from ADC channel 0
set_adc_channel(1); // Select RA1
//delay_ms(20); // Wait to Read ADC
measuredvoltagedrop = read_adc(); // Read the Voltage dropped across the R from ADC channel 1
voltage = measuredvoltage/51; // Measured Voltage is 51 steps per Volt at a Reference Voltage of 5V
voltagedrop = measuredvoltagedrop/51;
voltagedifference = voltage - voltagedrop;
currentma = voltagedifference; // Calculating Current using 1K Resistance
powerold = power; // Calculate the Power from the inputs
power = voltage * currentma;
pwm = pwm + direction*delta; // Adjust Pulse Width Modulation Value by Delta value
if (power < powerold) // If at top of curve, change direction
{
direction = -direction;
continue;
}
if (pwm > upperbound) // If at maximum PWM, Stop here
{
pwm = upperbound;
continue;
}
if (pwm < lowerbound) // If at minimum PWM, Stop here
{
pwm = lowerbound;
continue;
}
set_pwm1_duty(pwm); // Set PWM Mark-Space Radio to approx 50%
}
}
#device ADC = 8 // 一个8位寄存器ADC模式
#fuses HS, NOWDT, PUT, NOPROTECT, BROWNOUT, NODEBUG, NOLVP // High-Speed 20MHz, No Watchdog, No Protection, Brownout Protection,
#use delay(clock=20000000) // 20MHz Crystal
//int is defined as 8-bit unsigned integer using CCS compiler
void main (void)
{
signed int direction;
int delta;
int pwm;
int upperbound;
int lowerbound;
float power;
float powerold;
float voltage;
float voltagedrop;
float voltagedifference;
float currentma;
float measuredvoltage;
float measuredvoltagedrop;
direction = 1; // Set initial direction to positive
delta = 1; // Amount by which to adjust the PWM - 7-bit resolution so duty step of 2%
pwm = 26; // Initial position of the PWM - 50% Duty Cycle with 7-bit resolution.
upperbound = 49; // Upper bound of the PWM %
lowerbound = 1; // Lower bound of the PWM %
power = 0; // Initial Value of Power
setup_adc(ADC_CLOCK_DIV_32); // ADC clock
setup_adc_ports(ALL_ANALOG); // Set all inputs to analog
output_low(PIN_C1); // Set CCP1 output low
setup_ccp1(ccp_pwm); // setup pin CCP1 (RC2) to do PWM
setup_timer_2(T2_DIV_BY_1,12,1); // 384.615kHz
while (1)
{
//delay_ms(1000) // Wait 1 Second
set_adc_channel(0); // Select RA0
//delay_ms(20); // Wait to Read ADC
measuredvoltage = read_adc(); // Read the voltage input from ADC channel 0
set_adc_channel(1); // Select RA1
//delay_ms(20); // Wait to Read ADC
measuredvoltagedrop = read_adc(); // Read the Voltage dropped across the R from ADC channel 1
voltage = measuredvoltage/51; // Measured Voltage is 51 steps per Volt at a Reference Voltage of 5V
voltagedrop = measuredvoltagedrop/51;
voltagedifference = voltage - voltagedrop;
currentma = voltagedifference; // Calculating Current using 1K Resistance
powerold = power; // Calculate the Power from the inputs
power = voltage * currentma;
pwm = pwm + direction*delta; // Adjust Pulse Width Modulation Value by Delta value
if (power < powerold) // If at top of curve, change direction
{
direction = -direction;
continue;
}
if (pwm > upperbound) // If at maximum PWM, Stop here
{
pwm = upperbound;
continue;
}
if (pwm < lowerbound) // If at minimum PWM, Stop here
{
pwm = lowerbound;
continue;
}
set_pwm1_duty(pwm); // Set PWM Mark-Space Radio to approx 50%
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?

2025-03-06 广告
随机振动测试是评估产品或结构在随机振动环境下性能和可靠性的重要方法。在华南包装技术(东莞)有限公司,我们专注于此类测试,通过模拟真实使用中的随机振动条件来确保产品包装的稳固性。测试涉及设定频率范围、功率谱密度等参数,利用振动台和控制系统对试...
点击进入详情页
本回答由华南检测机构提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询