使用vim编辑编译c51程序

不准备考研了,所以决定学一下单片机什么的,不过今天终于受不了kei那差的要死的编辑器了,在网上发现了一个相当漂亮还能自动补全的软件sublime_text,功能也很强大,不过要想实现在这个软件中编译51程序好像还得学习一下它的插件制作教程,需要用到我感兴趣的python,不过资料太少,最终还是决定用vim,搞了一晚上终于把基本功能搞定了。
 
使用的vim版本:703
使用的Keil版本:Keil4
 
目前支持sbit,sfr等51 c特有关键字与uint,uchar的高亮,只可以编译单个的51c文件
 
以下 “~” 代表Vim所在目录
1,将keil\c51\bin加入到系统路径中,确保在命令行中可以使用c51.exe bl51.exe oh51.exe
2,打开~\vim73\filetype.vim 在大概200行左右插入:
 
    au BufNewFile,BufRead *.c51 setf c51
 
3,在~\vim73\syntax目录下新建c51.vim,拷贝c.vim中的内容,在172行加入:
    syn keyword cType        sfr sfr16 bit sbit 
    syn keyword cType        uchar uint
 
4,在~\vim73\ftplugin目录下新建c51.vim:
" Vim filetype plugin file
" Language: 51c
" Maintainer: w-gh <wgh1992@gmail.com>
" Last Change: 2013 Mar 21
 
"Only do this when not done yet for this buffer
if exists("b:did_ftplugin")
    finish
endif
 
" Don't load another plugin for this buffer
let b:did_ftplugin = 1
 
" Using line continuation here.
let s:cpo_save = &cpo
 
setlocal softtabstop=4
 
"定义当前脚本有效的变量
let s:lastShellReturn_C = 0        "最近一次编译的返回值
let s:lastShellReturn_L = 0        "最近一次链接的返回值
let s:ShowWarning = 1
 
function Compiple()
    exe ":ccl"                  
    "关闭快速修改窗口
    exe "update"               
    "文件如果有修改就进行保存
    let s:LastSheelReturn_C = 0
    let Sou = expand("%:p")        "文档的完整路径
    let Obj = expand("%:p:r").s:Obj_Extension    "obj文件的路径
    let v:statusmsg = ''            "vim预定义变量
    "let Mp = &makeprg
    if !filereadable(Obj) || ((filereadable(Obj) && getftime(Obj) < getftime(Sou)))
        redraw!                    "更新屏幕
        setlocal makeprg=c51\ %
        "10sleep
        echohl WarningMsg | echo " compiling...."
        silent make
        redraw!
        if v:shell_error != 0
            let s:LastSheelReturn_C = v:shell_error
        endif
 
        if s:LastSheelReturn_C != 0
            exe ":bo cope"
            echohl WarningMsg | echo " compilation failed"
        else
            if s:ShowWarning
                exe ":bo cw"
            endif
            echohl WarningMsg | echo " compilation successful"
        endif
    else
    "    echohl WarningMsg | echo "".Obj_Name."is up to date"
    endif
    "exe ":setlocal makeprg=Mp
endfunc
 
function Link()
    if s:LastSheelReturn_C != 0
        return
    endif
    let s:LastSheelReturn_L = 0
    "let Mp = &makeprg
    let v:statusmsg = ''
    setlocal makeprg=bl51\ %<.obj\ to\ %<.abs
    echohl WarningMsg | echo " Creating Abs file...."
    silent make
    redraw!
    if v:shell_error != 0
        let s:LastSheelReturn_L = v:shell_error
        exe ":bo cope"
        echohl WarningMsg | echo "Creating Abs file failed"
    else
        if s:ShowWarning
            exe ":bo cw"
        endif
        echohl WarningMsg | echo " Creating Abs file successful"
    endif
    "setlocal makeprg=Mp
endfunction
 
function CreatHex()
    if s:LastSheelReturn_C != 0 || s:LastSheelReturn_L != 0
        return
    endif
    "let Mp = &makeprg
    let v:statusmsg = ''
    setlocal makeprg=oh51\ %<.abs
    echohl WarningMsg | echo " Creating Hex file...."
    silent make
    redraw!
    if v:shell_error != 0
        exe ":bo cope"
        echohl WarningMsg | echo "Creating Hex file failed"
    else
        if s:ShowWarning
            exe ":bo cw"
        endif
        echohl WarningMsg | echo " Creating Hex file successful"
    endif
    "setlocal makeprg=Mp
endfunction
 
function Run()
    call Compiple()
    call Link()
    call CreatHex()
endfunction
 
map <F9> :call Run()<CR>
 
let &cpo = s:cpo_save
unlet s:cpo_save
4,现在创建一个以.c51为后缀的文件,编辑完成后按F9键进行编译连接,并生成hex文件。
 
参考资料:
    1,Vim.User.Manual




posted @ 2013-03-21 04:38  HooWxxd  阅读(458)  评论(0)    收藏  举报