I'm trying to write a code that will display the character H in the left top corner when the potentiometer is fully counter-clockwise, H in the middle when potentiometer is halfway between ccw and cw, and finally H in the right top corner when potentiometer is fully clockwise. Though my code works, I used a for loop to iterate through all the LEDS before the place where I want to put the H character. I wanted to know how I can change the position of the column number of the Oled.
Here's my code:
#include<p32xxxx.h>
#include<plib.h>
#pragma config POSCMOD=HS, FNOSC = PRIPLL
#pragma config FPLLIDIV = DIV_4
#pragma config FPLLMUL = MUL_16
#pragma config FPLLODIV = DIV_4
#pragma config FWDTEN = OFF
#pragma config FPBDIV=DIV_4
#define prtVddCtrl IOPORT_F
#define prtVbatCtrl IOPORT_F
#define prtDataCmd IOPORT_F
#define prtReset IOPORT_G
#define bitVddCtrl BIT_6
#define bitVbatCtrl BIT_5
#define bitDataCmd BIT_4
#define bitReset BIT_9
#define cbOledDispMax 512 //max number of bytes in display buffer
#define ccolOledMax 128 //number of display columns
#define crowOledMax 32 //number of display rows
#define cpagOledMax 4
#define POT 2
void initADC(int amask){
AD1PCFG = amask;
AD1CON1=0x00E0;
AD1CSSL=0;
AD1CON2=0;
AD1CON3=0x1F3F;
AD1CON1SET = 0x8000;
}
int readADC(int ch)
{
AD1CHSbits.CH0SA = ch;
AD1CON1bits.SAMP=1;
while(!AD1CON1bits.DONE);
return ADC1BUF0;
}
BYTE rgb0ledBmp[cbOledDispMax];
void DelayMs(int n){
int k = 0;
for(k=0;k<n*10;k++){
}
}
void OledHostInit() {
unsigned int tcfg; /* Initialize SPI port 2. */
SPI2CON = 0;
SPI2BRG = 15; //8Mhz, with 80Mhz PB clock
SPI2STATbits.SPIROV = 0;
SPI2CONbits.CKP = 1;
SPI2CONbits.MSTEN = 1;
SPI2CONbits.ON = 1; /* Make pins RF4, RF5, and RF6 be outputs. */
PORTSetBits(IOPORT_F, bitVddCtrl|bitVbatCtrl|bitDataCmd);
PORTSetPinsDigitalOut(prtDataCmd, bitDataCmd); //Data/Command# select
PORTSetPinsDigitalOut(prtVddCtrl, bitVddCtrl); //VDD power control (1=off)
PORTSetPinsDigitalOut(prtVbatCtrl, bitVbatCtrl); //VBAT power control (1=off) /* Make the RG9 pin be an output. On the Basic I/O Shield, this pin ** is tied to reset. */
PORTSetBits(prtReset, bitReset);
PORTSetPinsDigitalOut(prtReset, bitReset); }
BYTE Spi2PutByte(BYTE bVal) {
BYTE bRx; /* Wait for transmitter to be ready */
while (SPI2STATbits.SPITBE == 0); /* Write the next transmit byte. */
SPI2BUF = bVal; /* Wait for receive byte. */
while (SPI2STATbits.SPIRBF == 0); /* Put the received byte in the buffer. */
bRx = SPI2BUF;
return bRx; }
void OledDspInit() { /* We're going to be sending commands, so clear the Data/Cmd bit
*/ PORTClearBits(prtDataCmd, bitDataCmd);
/* Start by turning VDD on and wait a while for the power to come up. */
PORTClearBits(prtVddCtrl, bitVddCtrl);
DelayMs(1); /* Display off command */
Spi2PutByte(0xAE); /* Bring Reset low and then high */
PORTClearBits(prtReset, bitReset);
DelayMs(1);
PORTSetBits(prtReset, bitReset); /* Send the Set Charge Pump and Set Pre-Charge Period commands */
Spi2PutByte(0x8D);
Spi2PutByte(0x14);
Spi2PutByte(0xD9);
Spi2PutByte(0xF1); /* Turn on VCC and wait 100ms */
PORTClearBits(prtVbatCtrl, bitVbatCtrl);
DelayMs(100); /* Send the commands to invert the display. This puts the display origin ** in the upper left corner. */
Spi2PutByte(0xA1); //remap columns
Spi2PutByte(0xC8); //remap the rows /* Send the commands to select sequential COM configuration. This makes the ** display memory non-interleaved. */
Spi2PutByte(0xDA); //set COM configuration command
Spi2PutByte(0x20); //sequential COM, left/right remap enabled /* Send Display On command */
Spi2PutByte(0xAF); }
void OledPutBuffer(int cb, BYTE * rgbTx) {
int ib;
BYTE bTmp; /* Write/Read the data */
for (ib = 0; ib < cb; ib++) { /* Wait for transmitter to be ready */
while (SPI2STATbits.SPITBE == 0); /* Write the next transmit byte. */
SPI2BUF = *rgbTx++; /* Wait for receive byte. */
while (SPI2STATbits.SPIRBF == 0);
bTmp = SPI2BUF;
}
}
void OledUpdate() {
int ipag;
int icol;
BYTE * pb;
pb = rgb0ledBmp;
for (ipag = 0; ipag < cpagOledMax; ipag++) {
PORTClearBits(prtDataCmd, bitDataCmd); /* Set the page address */
Spi2PutByte(0x22); //Set page command
Spi2PutByte(ipag); //page number /* Start at the left column */
Spi2PutByte(0x00); //set low nybble of column
Spi2PutByte(0x10); //set high nybble of column
PORTSetBits(prtDataCmd, bitDataCmd); /* Copy this memory page of display data. */
OledPutBuffer(ccolOledMax, pb);
pb += ccolOledMax; }
}
void CharTable(int index){
switch(index){
case 17:
//character H
Spi2PutByte(0x00);
Spi2PutByte(0xFF);
Spi2PutByte(0x10);
Spi2PutByte(0x10);
Spi2PutByte(0xFF);
Spi2PutByte(0x00);
break;
};
}
void setPageNumber(BYTE pagnum){
PORTClearBits(prtDataCmd, bitDataCmd); /* Set the page address */
Spi2PutByte(0x22); //Set page command
Spi2PutByte(pagnum); //page number /* Start at the left column */
Spi2PutByte(0x10); //set low nybble of column
Spi2PutByte(0x10); //set high nybble of column
PORTSetBits(prtDataCmd, bitDataCmd);
}
void main(){
int a;
int repeatccw=0;
int repeatcw=0;
int repeathor=0;
OledHostInit();
OledDspInit();
OledUpdate();
setPageNumber(0x00);
T3CON = 0x8070;
PR3 = 0xFFFF;
initADC(0xFFFB);
int k=0;
while(1){
a = readADC(POT);
a=a>>5;
if(a==0b00000){
if(repeatccw==0) repeatccw++;
else if(repeatccw!=0) continue;
OledUpdate();
setPageNumber(0x00);
CharTable(17);
repeathor=0;
repeatcw=0;
}
else if(a==0b10000){
if(repeathor==0) repeathor++;
else if(repeathor!=0) continue;
OledUpdate();
setPageNumber(0x00);
for(k=0;k<64;k++) Spi2PutByte(0x00);
CharTable(17);
repeatcw=0;
repeatccw=0;
}
else if(a==0b11111){
if(repeatcw==0) repeatcw++;
else if(repeatcw!=0) continue;
OledUpdate();
setPageNumber(0x00);
for(k=0;k<110;k++) Spi2PutByte(0x00);
CharTable(17);
repeathor=0;
repeatccw=0;
}
}
}
Question
Hashir
I'm trying to write a code that will display the character H in the left top corner when the potentiometer is fully counter-clockwise, H in the middle when potentiometer is halfway between ccw and cw, and finally H in the right top corner when potentiometer is fully clockwise. Though my code works, I used a for loop to iterate through all the LEDS before the place where I want to put the H character. I wanted to know how I can change the position of the column number of the Oled.
Here's my code:
Link to comment
Share on other sites
4 answers to this question
Recommended Posts
Archived
This topic is now archived and is closed to further replies.