Created
August 27, 2018 07:10
-
-
Save totegamma/720f71065e397815161e272a294c2deb to your computer and use it in GitHub Desktop.
use MCP4922 with pic18f4553
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* File: main.c | |
* Author: thotgamma | |
* | |
* Created on August 4, 2018, 4:31 AM | |
*/ | |
#pragma config FOSC = HS // 20MHz Xtal(分周なし) | |
#pragma config MCLRE = ON // リセットピンを利用する | |
#pragma config LVP = OFF // 低電圧プログラミング機能使用しない(OFF) | |
#pragma config WDT = OFF // ウォッチドッグタイマーを利用しない | |
#define _XTAL_FREQ 20000000 | |
#include <xc.h> | |
#include <stdint.h> | |
#include <stdbool.h> | |
void SPIWrite(uint16_t data) { | |
uint8_t dummy; | |
LATBbits.LATB3 = 0; | |
SSPBUF = (uint8_t)((data & 0xFF00) >> 8); | |
while(!SSPSTATbits.BF); | |
dummy = SSPBUF; | |
SSPBUF = (uint8_t)(data & 0x00FF); | |
while(!SSPSTATbits.BF); | |
dummy = SSPBUF; | |
LATBbits.LATB3 = 1; | |
LATBbits.LATB2 = 0; | |
__delay_us(1); | |
LATBbits.LATB2 = 1; | |
} | |
void main(void) { | |
ADCON1 = 0b00001111; | |
TRISA = 0b00000000; | |
TRISB = 0b00000000; | |
TRISC = 0b00110000; //D-,D+ | |
TRISD = 0b00000000; | |
TRISE = 0b00000011; //SW1,2=INPUT LED=OUTPUT | |
LATA = 0b00000000; | |
LATB = 0b00001100; | |
LATC = 0b00000000; | |
LATD = 0b00000000; | |
LATE = 0b00000000; | |
SSPSTAT = 0b01000000; | |
SSPCON1 = 0b00100000; | |
uint16_t value = 0; | |
bool mode = false; | |
while (true) { | |
/* | |
if (mode) { | |
SPIWrite(0x7FFF); | |
} else { | |
SPIWrite(0x7000); | |
} | |
mode = ~mode; | |
*/ | |
// /* | |
SPIWrite(0x7000 | value); | |
value += 0xFF; | |
if (value >= 0x1000) value = 0; | |
// */ | |
} | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment