Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Pic18f4620 vga

Status
Not open for further replies.

AtomSoft

Well-Known Member
Hey guys im trying to learn this VGA Stuff but no luck. Here is my code. Can someone test it on a CRT? I have a Samsung 19in (FLAT) but not working so im not sure why tho. It looks almost perfect... Here is my code.

Im trying to use RED only and the resolution is 640*480 @ 60hz. I think my monitor doesnt support this??? is that possible?

The schematic is SIMPLE..
VGA Cable PINS USED:
1 = RED = TIE THIS TO VCC
2 = BLUE = TIE THIS TO VSS
3 = GREEN = TIE THIS TO VSS
10 = GND = TIE THIS TO VSS
13 = HSYNC = RB0
14 = VSYNC = RB1

Code:
/* *****************************************************************************
;                                                                             *
;    Filename:                     				                              *
;    Date:                                         	                          *
;    File Version: 001                                                        *
;                                                                             *
;    Author:   Jason Lopez                                                    *
;    Company:  AtomSoft                                                       *
;                                                                             *
;***************************************************************************** */

#include <p18f4620.h>
#include <delays.h>

#pragma config WDT = OFF, LVP = OFF, OSC = INTIO67, XINST = OFF

/************************************
Prototypes
*************************************/
void main(void);

//#define RED   LATBbits.LATB0
//#define GREEN LATBbits.LATB1
//#define BLUE  LATBbits.LATB2

#define HSYNC LATBbits.LATB0
#define VSYNC LATBbits.LATB1

/************************************
Main
*************************************/
void main(void){
	int x = 0x0000;
	OSCCON = 0x72;      			//8MHz clock
	while(!OSCCONbits.IOFS);		//wait for osc stable

	ADCON1 = 0x0F;			//Digital Pins (we dont need ADC)

	TRISB = 0x00;	//All Outputs

	LATB = 0x00;	//All Low

//H Refresh Rate: 37.861 khz aka 26.412 uS aka 52/53 Cycles
//V Refresh Rate: 72.809 hz aka 13.734 mS aka 27468 Cycles

	HSYNC = 9;
	VSYNC = 1;
	while(1){
//		RED = 1; //TIED HIGH and Green and Blue are tied LOW
 		for(x=0;x<480;x++){	
			Delay10TCYx(2);
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();	//25 us including FOR LOOP

			//SYNC FROM HERE
			Nop();
			Nop(); 		//1us
			HSYNC = 0;	
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
			HSYNC = 1;	//4us
			Nop();
			Nop();
			Nop();		//1.5us
			//SYNC DONE took 6.5us + 25us data = 31.5us Horizontal Timing
		}//15.12ms loop
		Delay10TCYx(70);	//350us before vertical sync 
		VSYNC = 0;			//Sync ready
		Delay10TCYx(11);	
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
		VSYNC = 1;			//60us Sync hold
		Delay10TCYx(203);	//1.015ms end
		//Vertical Sync done! took 1.425ms
		//Total 480 Lines loop * 31.5us per line + 1.425ms = 
		// 15.12ms + 1.425ms = 16.545ms total === 60.44 hz refresh rate...
	}
}
 

AtomSoft

Well-Known Member
I altered delays since i notice some bad VSYNC but still a no go. I used MPLAB Sim to test timing and all seems to fall into place right...
Sorry delays where right just got confused heh
 
Last edited:

AtomSoft

Well-Known Member
ok this is how i think it work. Sounds correct to me.
vga-jpg.47253
 

Attachments

  • VGA.jpg
    VGA.jpg
    125.2 KB · Views: 677

Gayan Soyza

Active Member
Hi Jason that's the one I was looking for.Now I got it what you & I'm going to do :D

I have seen for HSYNC & VSYNC you using the same RED,GREEN PORTB bits :O

I'll give a try on my VGA monitor & also I'll show my code it will takes some time..............
 
Last edited:

AtomSoft

Well-Known Member
Those are commented out. I only use HSYNC and VSYNC . The RED i have tied to VCC and BLUE AND GREEN are tied to GND. SO only red should show on entire screen
 

Gayan Soyza

Active Member
Hi Jason your code looks ok to me.I'm writing an asm version & going to test it on my CRT.I'll let you know.

But I'm wondering on these issues.

*Internal OSC - does it need to be precise XT instead !!
*Does the GND signals on R,G,B lines needed !!in your case pin6.Also pin5.
 
Last edited:

AtomSoft

Well-Known Member
I Made a new image hope it will help everyone who wants to implement VGA. It should be clearer now:

atomvga-jpg.47266
 

Attachments

  • AtomVGA.jpg
    AtomVGA.jpg
    47.4 KB · Views: 528

AtomSoft

Well-Known Member
My New code but doesnt work not sure why:
Code:
/* *****************************************************************************
;                                                                             *
;    Filename:                     				                              *
;    Date:                                         	                          *
;    File Version: 001                                                        *
;                                                                             *
;    Author:   Jason Lopez                                                    *
;    Company:  AtomSoft                                                       *
;                                                                             *
;***************************************************************************** */

#include <p18f4620.h>
#include <delays.h>

#pragma config WDT = OFF, LVP = OFF, OSC = INTIO67, XINST = OFF

/************************************
Prototypes
*************************************/
void main(void);

//#define RED   LATBbits.LATB0
//#define GREEN LATBbits.LATB1
//#define BLUE  LATBbits.LATB2

#define HSYNC LATBbits.LATB0
#define VSYNC LATBbits.LATB1

/************************************
Main
*************************************/
void main(void){
	int x = 0x0000;
	OSCCON = 0x70;      			//8MHz clock
	while(!OSCCONbits.IOFS);		//wait for osc stable

	ADCON1 = 0x0F;			//Digital Pins (we dont need ADC)

	TRISB = 0x00;	//All Outputs

	LATB = 0x00;	//All Low

//H Refresh Rate: 37.861 khz aka 26.412 uS aka 52/53 Cycles
//V Refresh Rate: 72.809 hz aka 13.734 mS aka 27468 Cycles

	HSYNC = 1;
	VSYNC = 1;

	//VSYNC
	Delay10TCYx(70);	//350us Front porch 
	VSYNC = 0;			
	Delay10TCYx(11);	
		Nop();
		Nop();
		Nop();
		Nop();
		Nop();
		Nop();
		Nop();
		Nop();
		Nop();
		Nop();
	VSYNC = 1;			//60us Sync length
	Delay100TCYx(20);
	Delay10TCYx(3);	
		Nop();
		Nop();
		Nop();
		Nop();
		Nop();
		Nop();
		Nop();
		Nop();
		Nop();
		Nop();			//1.02ms back porch
	while(1){
 		for(x=0;x<480;x++){	
			Delay10TCYx(2);
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();//25 us Active Video Time (includes for loop)
			Nop();
			Nop();//1us Front Porch
			HSYNC = 0;	
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
			HSYNC = 1;//4us Sync Pulse Length
			Nop();
			Nop();
			Nop();
			Nop();//2us back porch
		}
		//VSYNC
		Delay10TCYx(70);	//350us Front porch 
		VSYNC = 0;			
		Delay10TCYx(11);	
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
		VSYNC = 1;			//60us Sync length
		Delay100TCYx(20);
		Delay10TCYx(3);	
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();
			Nop();			//1.02ms back porch
}
}
 

Gayan Soyza

Active Member
Hi Jason I did some program but still I didn't check with my hardware..........I used a 16Mhz instead of 8Mhz.

Code:
;==============================================================================
;    Filename	:	VGA Display
;    Date	:	22-10-2010
;    Author	:	Gayan Soyza
;==============================================================================

     			LIST      	p=16F88		; list directive to define processor
     			#INCLUDE 	<P16F88.INC>	; processor specific variable definitions
			errorlevel	-302		; supress bank warnings

     __CONFIG    _CONFIG1, _CP_OFF & _CCP1_RB0 & _DEBUG_OFF & _WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_OFF & _PWRTE_ON & _WDT_OFF & _HS_OSC
     __CONFIG    _CONFIG2, _IESO_OFF & _FCMEN_OFF

    			cblock 	0x20
    			H_Lower_Count,H_Upper_Count
    			Del_350,Del_60,Del_500,Del_Bal
    			endc
    			
#define	HSYNC 	PORTB,6
#define	VSYNC 	PORTB,7

#define	RED 	PORTB,0
#define	GREEN 	PORTB,1
#define	BLUE 	PORTB,2

RESET     		org	0x0000            	; processor reset vector
          		clrf	STATUS
          		clrf	PORTA
          		clrf	PORTB
          		goto    Initialise_Ports      	; go to beginning of program
          		
          		org	0x0004
          		retfie
;======================================================================
; Initialise_Ports
;======================================================================
Initialise_Ports	clrf	INTCON			; disable interrupts
			bsf	STATUS,RP0		; B1	
			movlw	b'01100110'		; F=4Mhz,Mode=Internal RC,Frequency is stable
			movwf	OSCCON
			clrf	ANSEL			; turn off analog inputs & make it to digital	
			movlw	b'111'
			movwf	CMCON
			clrf	CVRCON
			clrf	PIE1
			clrf	PIE2
			clrf	TRISA
			clrf	TRISB
			bcf	STATUS,RP0		; B0
;-------------------------------------------------------------------------------------------
Init_Values		bsf	RED			; turn ON RED
			bcf	GREEN			; turn OFF GREEN
			bcf	BLUE			; turn OFF BLUE
			bsf	VSYNC			; Set Origin
			bsf	HSYNC			; //
			goto	Del_1mS
;=============================================================
;Line frequency		=	31469Hz
;Total Time per H-line	=	31.77uS
;Display Time per line	=	25.17uS

;OSC = 16 Mhz---->instruction cycle = 250nS
;------------------------------------------------------------------------------
;Horizontal Line : 25.5uS Display Time + 1uS Hold + 4uS Hsync + 1.5uS End Delay
;480 times repeat to make one field
;------------------------------------------------------------------------------
H_Line_Upper		call	Show_Data		; 500nS + 25uS data
			goto	$+1			; 500nS Hold time
			bcf	HSYNC			; 4uS Hsync Pulse
			goto	$+1			; //
			goto	$+1			; //
			goto	$+1			; //
			goto	$+1			; //
			goto	$+1			; //
			goto	$+1			; //
			goto	$+1			; //
			goto	$+1			; //
			bsf	HSYNC
			goto	$+1			; 500nS
			decfsz	H_Upper_Count,F		; 250nS end delay
			goto	H_Line_Upper		; 500nS
			;
H_Line_Lower		call	Show_Data		; 500nS + 25uS data
			goto	$+1			; 500nS Hold time
			bcf	HSYNC			; 4uS Hsync Pulse
			goto	$+1			; //
			goto	$+1			; //
			goto	$+1			; //
			goto	$+1			; //
			goto	$+1			; //
			goto	$+1			; //
			goto	$+1			; //
			goto	$+1			; //
			bsf	HSYNC
			goto	$+1			; 500nS
			decfsz	H_Lower_Count,F		; 250nS end delay
			goto	H_Line_Lower		; 500nS
;----------------------------------------------------------------------------
;Vertical Line : 350uS Delay + 60uS VSync + 1mS End Delay
;----------------------------------------------------------------------------
Del_350uS		nop				; 350uS delay
			goto	$+1
			goto	$+1
			decfsz	Del_350,F
			goto	Del_350uS
			;
V_Sync			bcf	VSYNC			; make Vsync Low
Del_60uS		nop				; 60uS delay
			goto	$+1
			goto	$+1
			decfsz	Del_60,F
			goto	Del_60uS
			bsf	VSYNC			; make Vsync High
;---------------------------------------------------------------------------------
;1mS delay is divided into 500uS + 500uS
;Within the 1st 500uS- H_line variables, Delay variables and new R,G,B data is loaded
;---------------------------------------------------------------------------------
Del_1mS			nop
Load_350uS		movlw	.175			; 350uS Del
			movwf	Del_350
Load_60uS		movlw	.30			; 60uS Del
			movwf	Del_60
			movlw	.250			; 500uS Del
			movwf	Del_500
Load_480		movlw	.240			; load 480 H-lines
			movwf	H_Upper_Count
			movwf	H_Lower_Count
			goto	$+1			; 3uS time
;Loads new data to the filed & this data will be shown on Show_Data time (25.17uS time)
Load_New_Data	;---
			;---
			;---
			movlw	.250			; ~500uS
			movwf	Del_Bal
			nop
			goto	$+1
			goto	$+1
			decfsz	Del_Bal,F
			goto	$-4
Del_500uS		nop				; other 500uS delay time
			goto	$+1
			goto	$+1
			decfsz	Del_500,F
			goto	$-4
			goto	H_Line_Upper
			
;===========================================================	
;100 pixels in Vertical side
;replace goto's with R,G,B data
;===========================================================
Show_Data		nop				; pixel 1
			nop				; pixel 2
			goto	$+1			; pixel 4
			goto	$+1			; pixel 6
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1
			goto	$+1			; pixel 100
			return				; 500nS Hold time
			
          		end
 
Last edited:

Mr RB

Well-Known Member
It's a pretty difficult task with a PIC.

Here's my attempt;
**broken link removed**
On this page; Some EasyPIC6 projects

I was able to get 10 megapixel/sec output using some assembler table lookups mixed through MikroC code and some clever use of the MSSP port for sending the pixel data out using the PIC hardware. And colour.... Well colour slected for each line, I cheated. :D

VGA is one of those things with a PIC that is kind of easy to get a couple of lines on the screen, harder to get the lines rock solid stable synced, and gets real hard trying to send rock solid video data out in real time...

Good luck! I'm curious to see what you can come up with! :)
 
Status
Not open for further replies.

Latest threads

New Articles From Engineer's Garage

Top