Skip to content

Instantly share code, notes, and snippets.

@mipsparc
Created May 19, 2018 04:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mipsparc/48eb7b06abcc596b2f00fea712ed3b06 to your computer and use it in GitHub Desktop.
Save mipsparc/48eb7b06abcc596b2f00fea712ed3b06 to your computer and use it in GitHub Desktop.
PWM制御鉄道模型コントローラの試作品の制御用PIC16F1向けプログラム。シリアルで入力されたDuty ratioで駆動する。Microchip MCCで生成されたハードウェア制御用メソッドを呼び出している
#include "mcc_generated_files/mcc.h"
#include <stdio.h>
void move(unsigned char direction)
{
if (direction == 1) {
PWM1_DutyCycleSet(639);
PWM1_LoadBufferSet();
} else {
PWM2_DutyCycleSet(639);
PWM2_LoadBufferSet();
}
__delay_ms(10);
}
void main(void)
{
// initialize the device
SYSTEM_Initialize();
// Enable the Global Interrupts
INTERRUPT_GlobalInterruptEnable();
// Enable the Peripheral Interrupts
INTERRUPT_PeripheralInterruptEnable();
unsigned int level = 0;
unsigned int new_level = 0;
unsigned char direction = 1;
unsigned char got_char;
dir2_SetLow();
PWM1_Start();
PWM1_DutyCycleSet(0);
PWM1_LoadBufferSet();
printf("init\n\r");
while (1)
{
got_char = EUSART_Read();
// if (PIR1bits.RCIF) {
// got_char = RCREGbits.RCREG;
// }
if (got_char < '!'){
if (direction == 1) {
if (level) {
PWM1_DutyCycleSet(level * 3 / 4);
PWM1_LoadBufferSet();
__delay_ms(500);
}
PWM1_DutyCycleSet(0);
PWM1_LoadBufferSet();
PWM1_Stop();
__delay_ms(500);
dir2_SetHigh();
__delay_ms(200);
PWM2_Start();
PWM2_DutyCycleSet(level);
PWM2_LoadBufferSet();
printf("houten to 2\n\r");
direction = 2;
if (level) {
move(direction);
}
} else {
if (level) {
PWM2_DutyCycleSet(level * 3 / 4);
PWM2_LoadBufferSet();
__delay_ms(500);
}
__delay_ms(500);
PWM2_DutyCycleSet(0);
PWM2_LoadBufferSet();
PWM2_Stop();
dir2_SetLow();
__delay_ms(200);
PWM1_Start();
PWM1_DutyCycleSet(level);
PWM1_LoadBufferSet();
printf("houten to 1\n\r");
direction = 1;
if (level) {
move(direction);
}
}
} else if (got_char < '~') {
new_level = (got_char - '!') * 5;
printf("%d", new_level);
}
if (new_level > level) {
move(direction);
}
//ADC1_GetConversion(channel_AN11)
level = new_level;
if (direction == 1) {
PWM1_DutyCycleSet(level);
PWM1_LoadBufferSet();
} else {
PWM2_DutyCycleSet(level);
PWM2_LoadBufferSet();
}
__delay_ms(50);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment